Results 1 to 7 of 7
  1. #1
    Isty is offline Member
    Join Date
    Oct 2011
    Location
    Netherlands
    Posts
    5
    Rep Power
    0

    Default Passing information between instances of classes

    Hey folks,

    Suppose I have a class A which creates an instance of class B, and an instance of class C.
    Is there an easy way for B to access variables of C (and is it possible without interaction with A)?
    Thanks in advance!
    Isty

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Passing information between instances of classes

    way for B to access variables of C
    Have A pass to B a reference to C, say in C's constructor.
    Otherwise the two classes could only get to each other's variables by making them static. Not a good solution.

  3. #3
    Isty is offline Member
    Join Date
    Oct 2011
    Location
    Netherlands
    Posts
    5
    Rep Power
    0

    Default Re: Passing information between instances of classes

    Yeah I was indeed thinking about making everything static, but that seemed kind of artificial.
    I am not at all familiar with references (gathering information right now), maybe you can give a solution for this example?

    Java Code:
    public class A {
    	public A(){
    		B b = new B();
    		C c1 = new C();
    	}
    }

    Java Code:
    public class B {
    	public B(){
    		//Would like to get the current value of x in c1
    	}
    }
    Java Code:
    public class C {
    	public int x;
    	
    	public C(){
    		x=1;
    	}
    
            //Don't know if getX() is gonna be of any use, but still:
    	public int getX(){
    		return x;
    	}
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Passing information between instances of classes

    Java Code:
    public class A {
        public A(){
            B b = new B();
            C c1 = new C(b);  // pass reference to b so c1 can see the B object
        }
    }

  5. #5
    Isty is offline Member
    Join Date
    Oct 2011
    Location
    Netherlands
    Posts
    5
    Rep Power
    0

    Default Re: Passing information between instances of classes

    Could you provide the actual code to make that reference? I haven't been able to figure it out myself. It would be very useful!

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Passing information between instances of classes

    Look at the code in post#4.
    A reference to a class B object is passed to the class C's constructor.

  7. #7
    Isty is offline Member
    Join Date
    Oct 2011
    Location
    Netherlands
    Posts
    5
    Rep Power
    0

    Default Re: Passing information between instances of classes

    Oh, I did not notice the change! Thanks alot, problem solved:)

Similar Threads

  1. Replies: 5
    Last Post: 04-27-2011, 10:14 PM
  2. main not passing information to created class
    By Teclis in forum New To Java
    Replies: 2
    Last Post: 04-19-2011, 09:58 PM
  3. Passing objects to classes
    By Catfish1 in forum New To Java
    Replies: 3
    Last Post: 11-30-2010, 06:27 PM
  4. Deleting instances of classes from a list
    By Gmurph03 in forum New To Java
    Replies: 8
    Last Post: 11-27-2009, 12:41 PM
  5. Passing variable information between classes
    By zen_to_go in forum New To Java
    Replies: 1
    Last Post: 10-30-2007, 09:09 PM

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
  •