Home » Java programming language

Java Class class getSimpleName() method with example

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

Class class getSimpleName() method

  • getSimpleName() method is available in java.lang package.
  • getSimpleName() method is used to return the simple name of the underlying class as given in the source code.
  • getSimpleName() 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.
  • getSimpleName() method does not throw an exception at the time of returning the name of classes.

Syntax:

    public String getSimpleName();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is String, it returns the following values based on the given cases,

  • It returns the simple name of the class when the underlying class is not anonymous.
  • It returns "" (empty string) when the underlying class is anonymous.

Example:

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

public class GetSimpleNameOfClass {
    public static void main(String[] args) {

        // Creating an instance of String
        String str = new String();

        // It returns the Class object represented by the String class
        Class cl = str.getClass();

        // It returns the name by using getName() method
        String str_name = cl.getName();
        System.out.println("String Name :" + str_name);

        // It returns simple name by using getSimpleName() method
        String str_simple_name = cl.getSimpleName();
        System.out.println("String Simple Name :" + str_simple_name);
    }
}

Output

String Name :java.lang.String
String Simple Name :String






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.