Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Simple class names with Class.getSimpleName()

Get a readable type or exception label without package or enclosing-class names.

Version: Java SE 21

Syntax

public String getSimpleName()

obj.getClass().getSimpleName()
Type.class.getSimpleName()

Class.getSimpleName() is a no-argument instance method on Class that returns a simple name as a String. obj.getClass() gets the object's runtime class, regardless of the variable's declared type; calling it on null throws NullPointerException. When you know the type in advance, use Type.class.getSimpleName(). For the nested class Main.Config, the result is Config, with no package or enclosing-class name. problem.getClass().getSimpleName() labels the actual exception class without hard-coding its name.

For an anonymous class the method returns "", not null. An array adds [] to its component type's simple name: String[][] has two pairs of brackets, and an array with an anonymous component type gives []. Synthetic class names can contain special characters such as $. Simple names can collide: use them as labels, not as type identifiers. getName() returns the binary name of an ordinary named class; compare the Class objects themselves to check type identity.

Examples

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Comparator;

public class Main {
    public static class Config {
        public int retries;
        public int priority;
    }

    public static void main(String[] args) throws IllegalAccessException {
        Config config = new Config();
        Class<?> type = config.getClass();
        System.out.println(type.getSimpleName());

        Integer configuredRetries = Integer.valueOf("3");
        Field[] fields = type.getDeclaredFields();
        Arrays.sort(fields, Comparator.comparing(Field::getName));
        for (Field field : fields) {
            String name = field.getName();
            if ("retries".equals(name)) {
                field.setInt(config, configuredRetries.intValue());
            }
            System.out.println(name + "=" + field.getInt(config));
        }

        RuntimeException problem = new IllegalArgumentException();
        System.out.println(problem.getClass().getSimpleName());
        System.out.println(String[][].class.getSimpleName());
        Object anonymous = new Object() {};
        System.out.println("anonymous=\"" + anonymous.getClass().getSimpleName() + "\"");
    }
}

Result

Config
priority=0
retries=3
IllegalArgumentException
String[][]
anonymous=""

The quotes on the last line make the anonymous class's empty name visible.

Related lessons

  • Java · Lesson 5 · Advanced · Version: 2
  • Java · Lesson 20 · Advanced · Version: 2
  • Java · Lesson 54 · Advanced · Version: 2
  • Java · Lesson 47 · Basic · Version: 2
  • Java · Lesson 49 · Basic · Version: 2
  • Java · Lesson 51 · Basic · Version: 2
  • Java · Lesson 33 · Intermediate · Version: 2
  • Java · Lesson 36 · Intermediate · Version: 2
  • Java · Lesson 37 · Intermediate · Version: 2

Sources