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  :  
com/bethecoder/tutorials/reflection/GetClassName.java 
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);
    System.out.println(num.getClass());
    
    //From class
    System.out.println(Integer.class);
    
    //From Class.forName
    System.out.println(Class.forName("java.lang.Integer"));
    
    Class<?> clazz = Integer.class;
    System.out.println("---------------------------");
    System.out.println(clazz.getName());
    System.out.println(clazz.getSimpleName());
  }

}
   

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



 
  


  
bl  br