Home » Java programming language

Java Class class getName() method with example

Class class getName() method: Here, we are going to learn about the getName() method of Class class with its syntax and example.
Submitted by Preeti Jain, on November 21, 2019

Class class getName() method

  • getName() method is available in java.lang package.
  • getName() method is used to return the name of the classes, an interface, a primitive type, a void type, an array class denoted by this Class object.
  • getName() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • getName() method does not throw an exception at the time of returning the name of classes or an interface.

Syntax:

    public String getName();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is String, it returns the name of the class or an interface.

Example:

// Java program to demonstrate the example 
// of String getName() method of Class 

public class GetNameOfClass {
    public static void main(String[] args) {
        Thread th = new Thread();

        // Get Class object of Thread
        Class cl = th.getClass();

        // It return the name of the class Thread
        String class_name = cl.getName();

        // Display Class Name
        System.out.println("Class Name :: " + class_name);
    }
}

Output

Class Name :: java.lang.Thread


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.