Wednesday, December 8, 2010

Determine if Class or Interface??

How to determine if a Class object represents a Class or Interface??

You can determine by calling isInterface method in Class-
Class cls = java.lang.String.class; 
// returns true as String is a class and not an Interface
boolean isClass = !cls.isInterface();
cls = java.lang.Cloneable.class; 
isClass = !cls.isInterface(); // false as Cloneable is an interface, note negation sign

So at runtime if you want to know about the entity you are working with, the Class or Interface, you can use the above method.

No comments:

Post a Comment