Skip to main content
gamedev.net
🔒 Locked

[java] Classes, reflection, etc...

Started by smitty1276 Jun 24, 2004 at 1:16 AM 4 replies 1k views
Original Post
smitty1276
smitty1276
How do I accomplish the following...

Vector theVector = new Vector();
Object obj = theVector;
    
Class theClass = Class.forName("java.util.Vector");

//the condition in the if statement is what I'm looking for
if (obj is an instance of the same class as theClass)
  //do something
I figured it would be best just to keep the post short. If anyone needs me to elaborate, I'd be happy to do so. Thanks.
smitty1276
smitty1276
Sorry, but I somehow completely missed the isInstance() method.

smitty1276
smitty1276
The instanceof operator doesn't work with the Class object, or at least it didn't when I tried it.

That's why they provide the isInstance() method for it.
ibawt
ibawt
Quote:
Original post by smitty1276
The instanceof operator doesn't work with the Class object, or at least it didn't when I tried it.

That's why they provide the isInstance() method for it.


That's because a Class isn't an instance.
MadLep
MadLep
Try:
if (obj.getClass().equals(theClass))
"I pretend to work, they pretend to pay me."

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.