We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everyone,
Most of us know how to declare a class, and make instances. Now what I want to know is whether it is possible to access an instance's information after it's been made. If it's possible, how do you do this?
-- MenteCode.
P.S. If you're wondering why I need this, its for a robotics simulation.
Answers
What about:
* http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#getClass()
* http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html
From what I see here, is that getClass() will return the class' general information, rather than info an instance has been assigned, so I don't think this is quite what I'm looking for, but close.
Not sure if this helps, but I need to access every instance currently existing, gather each of their individual parameters, then display it on screen in graphical form. I'm focusing on the instance access.
-- MenteCode
If that Class class doesn't have what kinda "info" you're looking for, got no idea what that is! 8-X
Anyways, if we wanna track an object, we have to store its reference at the moment of its instantiation! :-w
Moved from "Question about Code" to "How To", because I don't see any code in your message... Please, go to Categories and read the descriptions, thanks.
If that's not what you mean, you have to be more precise.
See also the Objects tutorial.
... That's so simple, I can't believe I haven't thought of it. Yes, that's what I was looking for.
One more thing to clear up, as the Objects tutorial doesn't say this directly: There's no way to have two instances of the same name, correct?
-- MenteCode
The goal of OOP is to be able to instantiate a class as many times we want/need!
An object is an instantiated class. And it involves dynamically allocate a memory block and get its pointer reference.
Oh, oops. I goofed. I meant to say two instances of the same name.
In the example PhiLho listed, there can't be two instances with the name "a".
The only safe rule is this: each object has a unique pointer reference (memory address) in the same computer! o->
Thanks to the both of you. End of Post.
a is not the name of this instance! An instance has no real name of its own... a is just the name of a variable having the reference of the instance.
You can do:
to have two distinct instances.
But you can also do:
and then, the new instance is referenced by both a and b.
Oh OK. Thanks for clearing that up.
As for "A b = a;", what's the point of that? I know this question is technically done, but is there a real purpose for this?
-- MenteCode
In the same scope, perhaps not. But look at this small example I've made and its comments: >:)
Also shared objects are useful for caching objects in local variables for performance reasons! :ar!