Java.lang Package Classes

Java Class - getName() Method



The java.lang.Class.getName() method returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String.

Syntax

public String getName()

Parameters

No parameter is required.

Return Value

Returns the name of the class or interface represented by this object.

Exception

NA.

Example:

In the example below, the java.lang.Class.getName() method returns the 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 name of the class
    String name = cls.getName();
    System.out.println("Class Name = " + name);
  }
}

The output of the above code will be:

Class Name = MyClass

❮ Java.lang - Class