getClass() method in java

In Java getClass() method inherited from the Object Class plays a crucial role in Object identification. By using getClass() we can get real-time class definition of an object.

public final Class<?> getClass()

By using this method we can access class-level properties like the fully qualified name of the class, methods information, constructors information, etc.

import java.lang.reflect.Method;

public class GetClassMethod {
    public static void main(String[] args) {
        Object o = new String("The Code hata");
        Class c = o.getClass();
        System.out.println("Fully qualified name of the class =" + c.getName());
        Method[] m = c.getDeclaredMethods();
        System.out.println("Method Information of class" + c.getName());
        for (Method m1:m){
            System.out.println(m1.getName());
        }
    }
}
getClass() method in java

Note-

  • After loading every class file JVM creates an object of that type java.lang.Class in the heap area. We can use this class object to get class level information.
  • We can use getClass() very frequently in reflections.

Similar Java Tutorials

1 thought on “getClass() method in java”

Leave a Comment