• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to compile two classes that need each other

 
Vierda Mila
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make sure that "com" is in your classpath.
 
Vierda Mila
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe,

Thank you, it works.
 
Marty Fried
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marty Fried
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49858
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javac -d . *.java


it is working well...............
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic