Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Object.getClass(): an object's runtime class

`getClass()` returns the `Class` object for an object's runtime class, regardless of the variable's declared type. It lets you test for an exact class match, but it does not reveal a generic instance's type arguments.

Version: Java SE 21

Syntax

public final Class<?> getClass()
// Instance call: Class<?> type = object.getClass();

Object.getClass() is a no-argument instance method declared as public final Class<?> getClass(). It returns a Class object representing the object's actual runtime class, not a string or the variable's declared type. You can call this.getClass() in an interface default method or call getClass() on a caught exception to identify its actual class. To print the name, use obj.getClass().getName().

For a receiver expression obj with static type T, the compiler types the result as Class<? extends E>, where E is the erasure of T. With Number n = 42;, you can assign n.getClass() to Class<? extends Number> without a cast, even though the returned Class object represents Integer.

For two non-null references, a.getClass() == b.getClass() tests whether the objects have exactly the same class, not whether the objects are equal. obj.getClass() == ArrayList.class compares the object's class with a class literal and excludes subclasses. Use obj instanceof List<?> if you want to accept any implementation of List. Calling getClass() on a null reference throws NullPointerException, whereas instanceof returns false for null.

Reading a value as Object still lets you discover its actual class. Because of type erasure, however, getClass() cannot tell new ArrayList<String>() from new ArrayList<Integer>(): both instances have the same runtime class.

Examples

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> words = new ArrayList<>();
        List<Integer> numbers = new ArrayList<>();
        Object value = words;

        System.out.println(value.getClass().getName());
        System.out.println(value.getClass() == ArrayList.class);
        System.out.println(value instanceof List<?>);
        System.out.println(value.getClass() == numbers.getClass());
    }
}

Result

java.util.ArrayList
true
true
true

Although `value` is declared as `Object`, the first line shows `java.util.ArrayList`. The remaining lines confirm the exact class, compatibility with `List<?>`, and the same runtime class for both lists despite their different type arguments.

Related lessons

  • Java · Lesson 5 · Advanced · Version: 2
  • Java · Lesson 13 · Advanced · Version: 2
  • Java · Lesson 20 · Advanced · Version: 2
  • Java · Lesson 47 · Basic · Version: 2
  • Java · Lesson 49 · Basic · Version: 2
  • Java · Lesson 51 · Basic · Version: 2
  • Java · Lesson 58 · Basic · Version: 2
  • Java · Lesson 4 · Intermediate · Version: 2
  • Java · Lesson 5 · Intermediate · Version: 2
  • Java · Lesson 8 · Intermediate · Version: 2
  • Java · Lesson 33 · Intermediate · Version: 2
  • Java · Lesson 36 · Intermediate · Version: 2

Sources