Java Reflection - Java Class.isInstance(Object obj)








Syntax

Class.isInstance(Object obj) has the following syntax.

public boolean isInstance(Object obj)

Example

In the following code shows how to use Class.isInstance(Object obj) method.

public class Main {
/*from www  . j  a v a2  s  .c  o  m*/
   public static void main(String[] args) {
     Class cls = Long.class;
        
     Long l = new Long(8);
     Double d = new Double(3.5);
       
     // checking for Long instance
     boolean retval = cls.isInstance(l);
     System.out.println(l + " is Long ? " + retval);

     // checking for Long instance
     retval = cls.isInstance(d);
     System.out.println(d + " is Long ? " + retval);        
   }
}

The code above generates the following result.