Results 1 to 8 of 8
  1. #1
    Ruddlesdin is offline Member
    Join Date
    Nov 2013
    Location
    Cambridge
    Posts
    5
    Rep Power
    0

    Default How do I send message from B to A if B was instantiated from A

    Hi all,
    New to the forum and Noobish with Java. Easy question for you ......
    If I instantiate class B from class A how do I then send a message from B to A. I have been coming across this problem when class B is a GUI using a JFrame which was instantiated from a starter class A. I cannot see class A from B unless I make class A a static class but I am sure there must be a better way to do this. Is there not some kind of parent child relationship between the A and B.
    I hope I have explained this clearly.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    929
    Rep Power
    2

    Default Re: How do I send message from B to A if B was instantiated from A

    "Sending a message" is vague. Do you mean "calling a method"? We might need to see some code to be able to understand your question.

    Edit: removed complex example, probably more confusing than what OP wants. I'll save it for later :)
    Last edited by SurfMan; 11-05-2013 at 03:01 PM.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: How do I send message from B to A if B was instantiated from A

    You terminology is confusing. Instantiating one class from another has nothing to do with parent/child relationships. If you are talking about accessing instances that is something different. If you are instantiating B from A and need B to reference stuff in A there are several options. First, you can pass an instance of A to B thru its constructor. Second, you can make B an inner class of A. Then B will have access to all of A's members. This is normally done if there is some sort of relationship between A and B. But not a hierarchical relationship.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,714
    Rep Power
    5

    Default Re: How do I send message from B to A if B was instantiated from A

    Quote Originally Posted by Ruddlesdin View Post
    I hope I have explained this clearly.
    Nope. Just post a code example.

    Quote Originally Posted by SurfMan View Post
    "Sending a message" is vague.
    Sounds like terminology from some alternative language... didn't Smalltalk define calling methods on classes as "sending messages" ?
    Last edited by gimbal2; 11-05-2013 at 04:15 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Ruddlesdin is offline Member
    Join Date
    Nov 2013
    Location
    Cambridge
    Posts
    5
    Rep Power
    0

    Default Re: How do I send message from B to A if B was instantiated from A

    Sorry about the terminology, I am sure that I will learn it better eventually. You seem to get what I mean though and I understand what you are saying about the inner class. How do I pass an instance of A through the constructor. I am not familiar with this.
    Just to explain a bit better then....
    If class A was a GUI with a textfield and with a button that when pressed instantiated a class B. If class B then wanted to write text in the textfield of Class A then I need to be able to see the instance of class A but I can't.

    Yes, instead of 'sending a message' I mean 'calling a method'.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: How do I send message from B to A if B was instantiated from A

    Quote Originally Posted by Ruddlesdin View Post
    How do I pass an instance of A through the constructor. I am not familiar with this.
    Just to explain a bit better then....
    OK, I feel generous today. You should check out the Oracle tutorials in my signature though. And buy a good book on Java.

    Java Code:
    public class CallBackExample {
       public static void main(String[] args) {
          CallBackExample cbe = new CallBackExample();
          cbe.method("This is being called directly in main");
          Foo foo = new Foo(cbe);
          foo.fooMethod("This is being called indirectly by Foo");
       }
       
       public void method(String s) {
          System.out.println("Method in CallBackExample: " + s);
       }
    }
    
    class Foo {
       CallBackExample cbe;
       public Foo(CallBackExample cbe) {
          this.cbe = cbe;
       }
       public void fooMethod(String s) {
          cbe.method(s);
       }
    }
    Regards,
    Jim
    Last edited by jim829; 11-05-2013 at 05:56 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    Ruddlesdin is offline Member
    Join Date
    Nov 2013
    Location
    Cambridge
    Posts
    5
    Rep Power
    0

    Default Re: How do I send message from B to A if B was instantiated from A

    Quote Originally Posted by jim829 View Post
    OK, I feel generous today. You should check out the Oracle tutorials in my signature though. And buy a good book on Java.

    Java Code:
    public class CallBackExample {
       public static void main(String[] args) {
          CallBackExample cbe = new CallBackExample();
          cbe.method("This is being called directly in main");
          Foo foo = new Foo(cbe);
          foo.fooMethod("This is being called indirectly by Foo");
       }
       
       public void method(String s) {
          System.out.println("Method in CallBackExample: " + s);
       }
    }
    
    class Foo {
       CallBackExample cbe;
       public Foo(CallBackExample cbe) {
          this.cbe = cbe;
       }
       public void fooMethod(String s) {
          cbe.method(s);
       }
    }
    Regards,
    Jim
    Thank you Jim, this is new to me but it does make sense. I am trying to self teach myself Java and it has not been easy so I appreciate this little bit help in my understanding. I will check out the Java tutorials in your signature. As for the good book what do you recommend.
    Thanks
    Paul.

  8. #8
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,714
    Rep Power
    5

    Default Re: How do I send message from B to A if B was instantiated from A

    If you like a book that is to the point: Java The Complete Reference
    If you like to learn through puzzles and games: Head First Java 2nd edition (targets Java 5, but is still relevant)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. message could not be send to socket
    By voipp in forum Networking
    Replies: 0
    Last Post: 10-26-2013, 11:13 PM
  2. Replies: 8
    Last Post: 05-09-2012, 08:15 PM
  3. Send XML message over UDP?
    By udpkillsme in forum XML
    Replies: 2
    Last Post: 06-22-2011, 06:25 PM
  4. Send AMF message from client
    By nguyenkimhuyit in forum Networking
    Replies: 0
    Last Post: 04-22-2011, 09:27 AM
  5. Send error message from servlet to jsp
    By karq in forum Java Servlet
    Replies: 4
    Last Post: 07-02-2010, 10:07 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •