Listing 1: ClassName.java — Illustrates Class objects

interface Inter
{}

class Super
{}

class Sub extends Super
{}

class ClassName
{
   public static void displayClassName(Object o)
   {
      System.out.println(o.getClass());
   }

   public static void main(String[] args)
   {
      // Verify dynamic type is obtained
      Super s = new Sub();
      displayClassName(s);

      // Primitives have a Class object too
      Class c = int.class;
      System.out.println(c);
      
      // So do interfaces
      try
      {
         c = Class.forName("Inter");
         System.out.println(c);
      }
      catch (ClassNotFoundException x)
      {
         System.out.println(x);
      }

      // As do arrays of a given type
      float[] arr = new float[4];
      displayClassName(arr);

      Super[] arr2 = new Super[5];
      displayClassName(arr2);
   }
}

/* Output:
class Sub
int
interface Inter
class [F
class [LSuper;
*/
— End of Listing —