Archived

This topic is now archived and is closed to further replies.

Xorcist

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

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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"; }
}


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
>> 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

Share this post


Link to post
Share on other sites