Java Utility Library

Java Object - getClass() Method



The java.util.Object.getClass() method returns the runtime class of this Object. The returned Class object is the object that is locked by static synchronized methods of the represented class.

Syntax

public final Class<?> getClass()

Parameters

No parameter is required.

Return Value

Returns the Class object that represents the runtime class of this object.

Exception

NA

Example:

In the example below, the java.util.Object.getClass() method returns the Class object representing the runtime class of the given object.

import java.lang.*;
import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a vector Object
    Vector<Integer> obj1 = new Vector<Integer>();

    //populating obj1
    obj1.add(10);
    obj1.add(20);
    obj1.add(30);

    //printing the Class object of obj1 
    System.out.println("obj1 belongs to: " + obj1.getClass());

    //creating Object
    Float obj2 = 10.5f;

    //printing the Class object of obj2 
    System.out.println("obj2 belongs to: " + obj2.getClass());
  }
}

The output of the above code will be:

obj1 belongs to: class java.util.Vector
obj2 belongs to: class java.lang.Float

❮ Java.lang - Object