[java] Determining an Object's Class (How can you?)

Started by
6 comments, last by Xorcist 22 years, 4 months ago
Is there an easy way to determine what an instantiated Object''s class is? All I''m looking for is the name of the class? So far I haven''t been able to figure out just how to do this. Can anyone help out? Thanks.
Advertisement
you can find an objects type/class, but not the class''s name, it''s called RTTI (Run Time Type Identification), it''s not enabled in VC++ by default so make sure you enable it.

trying to find a good example in stroustrup''s book (its in chapter 15 i think), use typeid().
Abdulla, this is a Java forum . However, I don''t know enough Java to answer this myself.

[Resist Windows XP''s Invasive Production Activation Technology!]
Look at 'reflection' : java.lang.reflect.*
There is defined the Class, Method, Member, Field ... classes.

So, you use getClass on an object, and then can do stuff like print its name, call the members indirectly (via a class object)...

It also enables you to dynamically add classes (LoadClass thingys).

Edit: Disclaimer: I am not a Java programmer, I just have a book.

Edited by - Fruny on December 11, 2001 10:48:06 PM
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
object.getClass().getName()

You don't need reflection just to query the object's class.

"So crucify the ego, before it's far too late. To leave behind this place so negative and blind and cynical, and you will come to find that we are all one mind. Capable of all that's imagined and all conceivable."
- Tool
------------------------------


Edited by - wayfarerx on December 11, 2001 10:52:59 PM
"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut
You can use the instanceof operator to determine if an object is of a specific, known type:

if (object instanceof MyCoolObject)
{
...
}

If the object supports reflection, then you can obtain its class info:

Object o;
Class c = o.getClass();
String name = c.getName();

If you just want a text string, and it''s a class that you have defined, you could add your own getName method and avoid having to worry about reflection:

class MyCoolObject
{
public static String getName() { return ("MyCoolObject"; }
}


Technically, obj.getClass().getName() IS using reflection, because getClass() returns an instance of java.lang.reflect.Class, so reflection is being used... Just because you don''t need to import the package doesn''t mean that reflection isn''t being used.

Secondly, there''s NO reason to duplicate what the above code does with code of your own. This is java. There is no "if the object supports reflection." EVERY object supports reflection.
>> Technically, obj.getClass().getName() IS using reflection, because getClass() returns an instance of java.lang.reflect.Class >>

Check the API again. Object.getClass() returns an instance of java.lang.Class, not java.lang.reflect.Class, so you are not using reflection when doing this.

Henry

This topic is closed to new replies.

Advertisement