Java.lang Package Classes

Java Class - getSimpleName() Method



The java.lang.Class.getSimpleName() method returns the simple name of the underlying class as given in the source code. Returns an empty string if the underlying class is anonymous.

Syntax

public String getSimpleName()

Parameters

No parameter is required.

Return Value

Returns the simple name of the underlying class.

Exception

NA.

Example:

In the example below, the java.lang.Class.getSimpleName() method returns the simple name of the given class.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    MyClass x = new MyClass();
    Class cls = x.getClass();

    //print the simple name of the class
    String name = cls.getSimpleName();
    System.out.println("Class SimpleName = " + name);
  }
}

The output of the above code will be:

Class SimpleName = MyClass

❮ Java.lang - Class