Java getClass

There exists a class by name Class in Java, like you know, Double class and double. The object of class Class contain runtime representation of a class. Java getClass () method is used to know a class’s super classes and interfaces, its name etc. Following code explains.

Following is the class signature of class Class.

public final class Class extends Object

Each class, like Demo, Test, Integer, String etc., executed at runtime will be represented by a Class object by JVM. class Class is used extensively by Reflection API.

Let us write one program to show how to get an object of Class and to use its methods Java getClass ().
interface A {   }
interface B {   }
class C {   }

public class Demo extends C implements A, B
  public static void main(String args[])
    Integer i1 = new Integer(10);
    System.out.println("i1.getClass(): " + i1.getClass());    

    java.util.Date today = new java.util.Date();
    System.out.println("today.getClass(): " + today.getClass());   

    Demo d1 = new Demo();

    Class cls = d1.getClass();

    System.out.println("cls: " + cls);    
    System.out.println("cls.getName(): " + cls.getName());

    System.out.println("Demo super class: " + cls.getSuperclass());    

    Class cArray[] = cls.getInterfaces();
    for(Class c : cArray)
      System.out.println("Demo super interface: " + c);    

    System.out.println("Demo class loader: " + cls.getClassLoader());    
    System.out.println("Is Demo interface: " + cls.isInterface());    

Java getClass
Output screenshot on Java getClass Example

Class cls = d1.getClass();

The class Class does not have a constructor and for this reason we cannot create an object of class Class directly. The getClass() method of Object class returns an object of Class.

System.out.println(“cls: ” + cls);
System.out.println(“cls.getName(): ” + cls.getName());

The cls object Class represents the Demo class at runtime and prints the name of the class along with the word "class" like class Demo. The getName() method of Class returns String, just name of the class represented by cls; it prints simply Demo (not along with class word).

System.out.println(“Demo super class: ” + cls.getSuperclass());

getSuperclass() method of Class returns the super class of Demo. The above statement can be written as follows also.

Class c1 = cls.getSuperclass();

We can know the super interfaces of class Demo by calling getInterfaces() method of Class. The method returns an array of Class objects.

Class cArray[] = cls.getInterfaces();
for(Class c : cArray)
System.out.println(“Demo super interface: ” + c);

Using the new for loop, the interfaces implemented by Demo are printed.

System.out.println(“Demo class loader: ” + cls.getClassLoader());
System.out.println(“Is Demo interface: ” + cls.isInterface());

The getClassLoader() returns the class loader which loads the class Demo.

cls can be known whether it represents an interface using isInterface().

Leave a Comment

Your email address will not be published.