| Author |
How to compile two classes that need each other
|
Vierda Mila
Ranch Hand
Joined: Feb 25, 2008
Posts: 61
|
|
Dear all,
I have 2 classes and each class need object from each other as instance member. I have tried to compile these 2 classes and it always fail and compiler tell that cannot found symbol. it make sense because I fail to compile either class A or B.
My question is how to compile class like these according these 2 classes inside in same package, thank you for any kind help.
regards,
Vierda
|
SCJP 5
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
|
Make sure that "com" is in your classpath.
|
[My Blog]
All roads lead to JavaRanch
|
 |
Vierda Mila
Ranch Hand
Joined: Feb 25, 2008
Posts: 61
|
|
Christophe,
Thank you, it works.
|
 |
Marty Fried
Greenhorn
Joined: Feb 10, 2009
Posts: 18
|
|
Vierda Mila wrote:Dear all,
. . .
I'm curious about this pattern... I've never tried anything like that, and I'm wondering how it works. It seems like it would be an endless recursion or something; when you construct one class, it would need to construct the other one, which needs to construct the first one, etc.
So, I'm wondering - does this work? Am I making a simple mistake in my thinking (certainly not the first time)?
|
-- Marty Fried
Left Coast, USA
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24056
|
|
Hi Marty,
Well as written, neither class actually creates an instance of the other, so it's OK. If each one said something lke
A a = new A();
then yes indeed, it'd be infinite recursion and the program would blow up.
But there is more than one way to end up with a member variable pointing to to an object. For example, consider these two classes:
Here, an instance of A contains an instance of B that refers back to the same "A". If you use "new A()" to create an A object, only two objects are created, one A and one B, and they both know about each other.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Marty Fried
Greenhorn
Joined: Feb 10, 2009
Posts: 18
|
|
Ernest Friedman-Hill wrote:Hi Marty,
Well as written, neither class actually creates an instance of the other, so it's OK. If each one said something lke
A a = new A();
then yes indeed, it'd be infinite recursion and the program would blow up.
But there is more than one way to end up with a member variable pointing to to an object. For example, consider these two classes:
Here, an instance of A contains an instance of B that refers back to the same "A". If you use "new A()" to create an A object, only two objects are created, one A and one B, and they both know about each other.
Thanks, Ernest,
I think I see my mistake now; I'm still thinking in C++, and forgetting a basic difference, although I'm not certain if I'm right about C++. But I guess that in Java, you are only creating the reference to the object when you declare it like that, where in C++ you would actually be constructing the object. I realize that's the case, but I need to keep it in mind all the time.
Thanks for the example - that helped me realize my mistake.
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24056
|
|
Marty Fried wrote:
I think I see my mistake now; I'm still thinking in C++, and forgetting a basic difference, although I'm not certain if I'm right about C++. But I guess that in Java, you are only creating the reference to the object when you declare it like that, where in C++ you would actually be constructing the object. I realize that's the case, but I need to keep it in mind all the time.
Yep, you're right, the same program in C++ would indeed blow up. But the declaration "A a;" in Java is like "A *a" in C++, which would be safe too.
|
 |
Prince Chauda
Greenhorn
Joined: Feb 13, 2008
Posts: 8
|
|
Vierda Mila wrote:Dear all,
I have 2 classes and each class need object from each other as instance member. I have tried to compile these 2 classes and it always fail and compiler tell that cannot found symbol. it make sense because I fail to compile either class A or B.
My question is how to compile class like these according these 2 classes inside in same package, thank you for any kind help.
regards,
Vierda
The thing happening to this code that one class can't found other is due to the classpath.
Since neither of the file was compiled hence the directory having name "com" will not be there and thus your import statement will not work.
You can try this.
> If you want to compile A.java then put B.java in "com" directory and use the following code for compilation.
This code will also compile A.java.
but this will put your B.class in the current directory. Hence it will problematic in future so use this code for best result.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32668
|
|
And welcome to JavaRanch, Price Chauda
And thanks to Marty Fried for reminding us that C++ and Java are totally distinct languages.
|
 |
harilal ithikkat
Ranch Hand
Joined: Oct 06, 2008
Posts: 221
|
|
javac -d . *.java
it is working well...............
|
SCJP 1.5
"A candle looses nothing by lighting another candle"
itechmentors.com
|
 |
 |
|
|
subject: How to compile two classes that need each other
|
|
|