• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

how to call object in another class?

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what I came up with based on your question. Hope this helps.
 
Andrew Parker
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic