Easy Tutorial
❮ Java String Tochararray Java String Getchars ❯

Java Object getClass() Method

Java Object Class


The getClass() method of the Object class is used to obtain the runtime class of an object.

Syntax

object.getClass()

Parameters

Return Value

Returns the class of the object.

Example

The following example demonstrates the use of the getClass() method. Since String and ArrayList inherit from Object, they can directly use the getClass() method:

Example

import java.util.ArrayList;

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

    // getClass() with Object
    Object obj1 = new Object();
    System.out.println("Class of obj1: " + obj1.getClass());

    // getClass() with String
    String obj2 = new String();
    System.out.println("Class of obj2: " + obj2.getClass());

    // getClass() with ArrayList
    ArrayList<Integer> obj3 = new ArrayList<>();
    System.out.println("Class of obj3: " + obj3.getClass());
    }
}

The output of the above program is:

Class of obj1: class java.lang.Object
Class of obj2: class java.lang.String
Class of obj3: class java.util.ArrayList

Calling the getClass() method on a custom class:

Example

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

        // Create an object of tutorialproTest class
        tutorialproTest obj = new tutorialproTest();

        // tutorialproTest inherits from Object class, which is the superclass of all classes
        // Call getClass() method
        System.out.println(obj.getClass());
    }
}

The output of the above program is:

class tutorialproTest

Java Object Class

❮ Java String Tochararray Java String Getchars ❯