Identifying Objects using getClass()

K

klauern

How do I use .getClass() in an object to find out what class it is?

If I am using a Vector to store Apple and Orange objects, how do I find
out whether I threw in an Apple at position x or an Orange at position
x?
 
S

Steve W. Jackson

klauern said:
How do I use .getClass() in an object to find out what class it is?

If I am using a Vector to store Apple and Orange objects, how do I find
out whether I threw in an Apple at position x or an Orange at position
x?

In an instance, you can call this.getClass().getName() to find out the
class name. In any other code, having access to an instance (like the
one you stored in a Vector), you can do the same. That is, retrieve
from a Vector and do this:

Object o = myVector.get(index);
String className = o.getClass().getName();

If it's simple enough, you could simply get the item from the Vector and
ask whether "o instanceof Apple" or "o instanceof Orange" is true.

Multiple cat-skinning techniques are available. :)

= Steve =
 
R

R.F. Pels

klauern said:
How do I use .getClass() in an object to find out what class it is?
System.out.println(getClass().getName());

If I am using a Vector to store Apple and Orange objects, how do I find
out whether I threw in an Apple at position x or an Orange at position
x?


if (elem instanceof Apple)
{
...
}
else if
{
...
}
else
{
// Woah!
}
 
S

Steve Horsley

klauern said:
How do I use .getClass() in an object to find out what class it is?

If I am using a Vector to store Apple and Orange objects, how do I find
out whether I threw in an Apple at position x or an Orange at position
x?
Something like this should do the trick:

Object o = myVerctor.get(x);
if(o instanceof Apple) {
Apple a = (Apple) o;
// do stuff
}
else if(o instance of Orange) {
Orange b = (Orange o);
// do stuff
} else {
throw new GastlyMessException();
}

Steve
 
T

Thomas G. Marshall

Steve W. Jackson coughed up:
In an instance, you can call this.getClass().getName() to find out the
class name. In any other code, having access to an instance (like the
one you stored in a Vector), you can do the same. That is, retrieve
from a Vector and do this:

Object o = myVector.get(index);
String className = o.getClass().getName();

If it's simple enough, you could simply get the item from the Vector
and ask whether "o instanceof Apple" or "o instanceof Orange" is true.

Ah, so long as you are careful if Apple and Orange happen to be within the
same hierarchy.

The following:

obj instanceof Thing

will return true if obj is an instance of Thing, or an instance of any
subclass of Thing. It works with interfaces as well.
 
T

Thomas G. Marshall

Thomas G. Marshall coughed up:
Steve W. Jackson coughed up:

Ah, so long as you are careful if Apple and Orange happen to be
within the same hierarchy.

The following:

obj instanceof Thing

will return true if obj is an instance of Thing, or an instance of any
subclass

er.....superclass. sorry
 
T

Thomas G. Marshall

Thomas G. Marshall coughed up:
Thomas G. Marshall coughed up:

er.....superclass. sorry

Christ, one of those days.

Subclass.

Nothing like confusing the @#$% out of the OP....
 
I

iamfractal

klauern said:
How do I use .getClass() in an object to find out what class it is?

If I am using a Vector to store Apple and Orange objects, how do I find
out whether I threw in an Apple at position x or an Orange at position
x?

Hi, Klauern!

All those excellent answer notwithstanding ... yes, you know what's
coming: make sure you have a startling good reason for implementing
such runtime type-checking, as, if you have such checks scattered
throughout the code, they will all need to be updated when you start
adding Lemons.

And you just know you will add Lemons, don't you.

Lemons usually spell catastrophe in OO systems.

And don't get me started on Papayas ...

..ed

www.EdmundKirwan.com - Home of The Fractal Class Composition
 
S

Steve W. Jackson

"Thomas G. Marshall"
Steve W. Jackson coughed up:

Ah, so long as you are careful if Apple and Orange happen to be within the
same hierarchy.

The following:

obj instanceof Thing

will return true if obj is an instance of Thing, or an instance of any
subclass of Thing. It works with interfaces as well.

You're absolutely correct, which is why I didn't necessarily say that
was the best or preferred way, and indicated that more than one way of
solving the problem is available. :)

= Steve =
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top