tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > Reflection > Get Class Name

Get Class Name 

The following example shows how to get a reference to class object and accessing its name. Class provides the following APIs,

public String getSimpleName()
 - Returns the simple name of class as given in the source code. 
   For anonymous class it returns empty string.

public String getName()
 - Returns the name of the class along with package name.

File Name  :  
Author  :  Sudhakar KV
Email  :  [email protected]
package com.bethecoder.tutorials.reflection;

public class GetClassName {

   @param args
   @throws ClassNotFoundException 
  public static void main(String[] argsthrows ClassNotFoundException {
    //From instance
    Integer num = new Integer(6);
    //From class
    //From Class.forName
    Class<?> clazz = Integer.class;


It gives the following output,
class java.lang.Integer
class java.lang.Integer
class java.lang.Integer


bl  br