Object getClass() Method in Java

We can use getClass() method of java.lang.Object class to get the runtime class definition of a runtime class object. Prototype of getClass() method of java.lang.Object class is:-

public final native Class<?> getClass()

Return value:- The Class object that represents the runtime class of this object.

Let us first understand what is runtime class and the object of runtime class? How it is related to the getClass() method in Java.

  • Runtime class: The class that is loaded into JVM at execution time is called runtime class.
  • Runtime class object: Every class bytecode is stored using java.lang.Class object. This java.lang.Class object is called runtime class object.

After loading every .class file, JVM creates one object of java.lang.Class type in the heap area. We can use this object to get class-level information like class name, method information, constructor information, and e.t.c. The getClass() method of Object class is used very frequently in the reflection API.

Assume there is a class called “A”. Using the getClass() method we can get the runtime class object of the “A” class.

A a1 = new A();
Class cls = a1.getClass();

Java Object getClass() Method Example on Pre-defined classes,

import java.util.ArrayList;

public class Test {
  public static void main(String[] args) {
    
    String str = new String();
    System.out.println("Class of str: " + str.getClass());
    
    ArrayList al = new ArrayList();
    System.out.println("Class of al: " + al.getClass());
  }
}

Output:-

Class of str: class java.lang.String
Class of al: class java.util.ArrayList

Java Object getClass() Method Example on Custom classes,

class A { }

public class Test {
  public static void main(String[] args) {
    
    Test t1 = new Test();
    System.out.println("Class of t1: " + t1.getClass());
    
    A a1 = new A();
    System.out.println("Class of a1: " + a1.getClass());
  }
}

Output:-

Class of t1: class Test
Class of a1: class A

Get Class Name in Java

To get the name of a Java class, call the getName() method on the runtime class object. This getName() method is defined in java.lang.Class class. Its prototype is:-

public String getName()

Assume there is a class called “A”. Below code shows retrieving the class name of runtime class object using the getClass() method in Java:-

// create object of class
A a1 = new A();

// get runtime object
Class cls = a1.getClass();

// get class name
String name = cls.getName();

The above lines of code can be written in a single line as follows:-

getclass().getname() Method in Java,

String name = a1.getClass().getName();

Java program to get the class name

class A { }

class Test {
  public static void main(String[] args) {

    Test t1 = new Test();
    String classname = t1.getClass().getName();
    System.out.println(classname);

    A a1 = new A();
    classname = a1.getClass().getName();
    System.out.println(classname);
  }
}

Output:-

Test
A

Using getClass() Method in Java to get Class Information

We can use the runtime class objects to get more information on the class. Some important methods are,

  • getFields()
  • getConstructors()
  • getMethods()

On calling the above methods on the runtime class object, they will give information of the current class, and all its superclass.

Java program to display all methods of a class using getClass() method of Object class,

import java.lang.reflect.*;
class Test {

  // methods
  public void m1(){}
  public void m2(){}
  public void m3(){}

  public static void main(String[] args) {

    Test t1 = new Test();
    Class cls = t1.getClass();

    // display class name of runtime class object
    String classname = cls.getName();
    System.out.println("Class Name: "+ classname);

    // display all methods of this Class object
    Method[] methods = cls.getMethods();
    System.out.println("Methods: ");
    for(Method m : methods) {
      System.out.print(m.getName() + " ");
    }
  }
}

Output:-

Class Name: Test
Methods:
m3 main m2 m1 wait wait wait equals toString hashCode getClass notify notifyAll

Test class in a direct child class of java.lang.Object class therefore methods of Objects class are also available to the child class.

Another simple way to display all methods of a class is by using javap tool. The javap is also a tool like ‘javac’ and ‘java’. The ‘javac’ and ‘java’ tools are used to compile and run the Java program. Open cmd and type “javap <classname>”.

> javap Test

Output:-

Compiled from “Test.java”
class Test {
Test();
public void m1();
public void m2();
public void m3();
public static void main(java.lang.String[]);
}

Different ways to get runtime class object in Java

In Java, the getClass() method is not the only way to get the runtime class object. Different ways to get the object of a given class are,

1) Class cls = classname.class;
2) Class cls = Class.forName(classname);
3) Class cls = obj.getClass();

Let us take a Test class for demonstration purposes,

public class Test {
  public static int i = m1();
  
  static {
    System.out.println("Test class: Static block executed");
  }
  
  public static void main(String[] args) {
    System.out.println("Test class: main method executed");
  }

  public static int m1() {
    System.out.println("Test class: Static variable initialized");
    return 5;
  }
}

1) Using classname.class to get the runtime class object,

// classname.class demonstration
public class Example1 {
  public static void main(String[] args) {
    Class<Test> t1 = Test.class;
    System.out.println("Name of class: " + t1.getName());
  }
}

Output:-

Name of class: Test

In the first approach, on using Test.class => Test class is loaded & stored to the JVM, and runtime class object returned.

In this approach, the Test class static variables are not initialized, static block and the main method of Test class is also not executed.

2) Using Class.forName() to get the runtime class object,


// Class.forName() demonstration
public class Example2 {
  public static void main(String[] args) 
      throws ClassNotFoundException {
    Class<?> t1 = Class.forName("Test");
    System.out.println("Name of class: " 
                      + t1.getName());
  }
}

Output:-

Test class: Static variable initialized
Test class: Static block executed
Name of class: Test

In this approach, Class.forName("Test"); => The Test class is loaded, java.lang.Class object is created, static variables of Test class are initialized. and static blocks of the Test class are also executed. Finally, the runtime class object is returned. The main method will not be executed.

3) Using getClass() to get the runtime class object,

// getClass() demonstration
public class Example3 {
  public static void main(String[] args){

    // creating object of class
    System.out.println("Creating an object of Test class");
    Test t1 = new Test(); 
    System.out.println("Test class object is created");

    // get class reference
    Class cls = t1.getClass();
    System.out.println("getClass() is called");
    
    // get name of class
    System.out.println("Name of class: " + cls.getName());
  }
}

Output:-

Creating an object of Test class
Test class: Static variable initialized
Test class: Static block executed
Test class object is created
getClass() is called
Name of class: Test

In this approach, the already created Class object reference is returned because the Test class has already loaded due to its object creation.

Whenever constructor is called for the first time (assuming class was not used before that statement) then the class is loaded, static variables are initialized, static blocks are executed. But getClass() is not performing these operations.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *