wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes how to call object in another class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to call object in another class?" Watch "how to call object in another class?" New topic
Author

how to call object in another class?

Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
Hi,
I have 3 classes. One contains the constructor (has 2 parameters), private variables, get and set methods. I instantiate an object in another class and pass the parameters to the constructor. So, the object's variables has values.
In the 3rd class, I want to display the values of the 2 variables of that object. How can I do it? It said no such variable found. But, in fact, it is an object, not variables.
How should I call the object and review it's variables' values through the get method?
Andrew
Frank Hale
Ranch Hand

Joined: Mar 25, 2000
Posts: 230
Here is what I came up with based on your question. Hope this helps.
Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
a my_a_object = my_b_object.getAObject();
So, I still need to reference my_b_object to my_a_object first and then call the getAObject method.
If I instantiate class a in class b, can I call the obj a directly (without referencing obj b) in test class?
Thanks for your kind help.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I have 3 classes. One contains the constructor (has 2 parameters), private variables, get and set methods.
Note that every class has one or more constructors. If you have not defined one in the class description (the .java file), then the compiler creates a "default" no-argument constructor for your class.
I instantiate an object in another class ... In the 3rd class, I want to display the values of the 2 variables of that object. How can I do it?
You must somehow get a reference to the object that you'd like to use. You'll likely do so by creating an instance of the class that you'd like to use, or by creating an instance of a class that defines objects that contain a reference to instances of the class that you'd like to use.
It said no such variable found. But, in fact, it is an object, not variables.
A variable is just an identifier. An identifier may be a reference to an Object or it may refer to a primitive value (such as int, char, double).
How should I call the object and review it's variables' values through the get method?
Again, you'll have to somehow get a reference to the object (that is an instance of some class) that you'd like to use.
------------
a my_a_object = my_b_object.getAObject();
So, I still need to reference my_b_object to my_a_object first and then call the getAObject method.

That's kinda what I've tried to explain so far. In this example, you'd first need to create an instance of class b in order to use any of its behaviors (instance methods).
If I instantiate class a in class b, can I call the obj a directly (without referencing obj b) in test class?
No (see above). If the getAObject were a static method of class b, (class b would have to otherwise redisigned and) then you wouldn't need an instance of class b in order to use the method.
Well, is this making any sense yet?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to call object in another class?