Results 1 to 7 of 7
- 10-02-2011, 01:50 PM #1
Member
- Join Date
- Oct 2011
- Location
- Netherlands
- Posts
- 5
- Rep Power
- 0
- 10-02-2011, 02:21 PM #2
Re: Passing information between instances of classes
Have A pass to B a reference to C, say in C's constructor.way for B to access variables of C
Otherwise the two classes could only get to each other's variables by making them static. Not a good solution.
- 10-02-2011, 02:38 PM #3
Member
- Join Date
- Oct 2011
- Location
- Netherlands
- Posts
- 5
- Rep Power
- 0
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; } }
- 10-02-2011, 02:43 PM #4
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 } }
- 10-02-2011, 03:07 PM #5
Member
- Join Date
- Oct 2011
- Location
- Netherlands
- Posts
- 5
- Rep Power
- 0
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!
- 10-02-2011, 03:44 PM #6
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.
- 10-02-2011, 05:55 PM #7
Member
- Join Date
- Oct 2011
- Location
- Netherlands
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
signer information does not match signer information of other classes
By swyatt in forum New To JavaReplies: 5Last Post: 04-27-2011, 09:14 PM -
main not passing information to created class
By Teclis in forum New To JavaReplies: 2Last Post: 04-19-2011, 08:58 PM -
Passing objects to classes
By Catfish1 in forum New To JavaReplies: 3Last Post: 11-30-2010, 05:27 PM -
Deleting instances of classes from a list
By Gmurph03 in forum New To JavaReplies: 8Last Post: 11-27-2009, 11:41 AM -
Passing variable information between classes
By zen_to_go in forum New To JavaReplies: 1Last Post: 10-30-2007, 08:09 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks