We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Object exists? (Read 1007 times)
Object exists?
Apr 16th, 2010, 5:28pm
 
Hey folks

I can't find a reference to a method that checks if an object has been instantiated. Something like ObjectReference.exist() that returns a boolean... is there such a thing?

Thanks a bunch
Re: Object exists?
Reply #1 - Apr 16th, 2010, 11:38pm
 
An object that has not been assigned to a "new something()"will be equal to null.
i.e.
if (myOb == null) {
 myOb = new Object();
}
Re: Object exists?
Reply #2 - Apr 21st, 2010, 2:32am
 
Thanks Noah! Should've thought of that... ^^

On a related topic, how can I test for the exact object reference, e.g.

Player myFriend = new Player ();
myMethod(myFriend);

void myMethod(Player friend) {
if (friend == myFriend) {...} // how can I compare two object references?
}
Re: Object exists?
Reply #3 - Apr 21st, 2010, 4:52am
 
The method you show is OK. == precisely compare object references, that's why in general it shouldn't be used to compare Strings... two identical strings of different origin (constant in code vs. file vs. loaded from net vs. build by concatenation, etc.) will have different references.
Re: Object exists?
Reply #4 - Apr 21st, 2010, 9:35am
 
Ah! Thanks. Howshould one compare strings then?
Re: Object exists?
Reply #5 - Apr 21st, 2010, 10:03am
 
string1.equals(string2)
That's the generic method to compare object by content instead of by reference. You might need to override equals() for your own classes.
Page Index Toggle Pages: 1