Java - how to tell class of an object?

Posted by lkm on Stack Overflow See other posts from Stack Overflow or by lkm
Published on 2010-05-06T00:37:43Z Indexed on 2010/05/06 0:48 UTC
Read the original article Hit count: 228

Filed under:
|
|

Given a method that accepts as a parameter a certain supertype. Is there any way, within that method, to determine the actual class of the object that was passed to it? I.e. if a subtype of the allowable parameter was actually passed, is there a way to find out which type it is? If this isn't possible can someone explain why not (from a language design perspective)? Thanks

Update: just to make sure I was clear

void doSomething(MyType myType) {
  //determine if myType is MyType OR one of its subclasses
}

Since the method signature specifies the parameter as being MyType, then how can one tell if the object is actually a subtype of MyType (and which one).

© Stack Overflow or respective owner

Related posts about java

Related posts about programming-languages