Close

Java Reflection - Class.getSimpleName() Examples

Code Snippets Java Reflection Java Java API 


Class:

java.lang.Class

java.lang.Objectjava.lang.Objectjava.lang.Classjava.lang.Classjava.io.SerializableSerializablejava.lang.reflect.GenericDeclarationGenericDeclarationjava.lang.reflect.TypeTypejava.lang.reflect.AnnotatedElementAnnotatedElementLogicBig

Method:

public String getSimpleName()

Returns the simple name of this class. Returns an empty string if this class is anonymous.

The simple name of an array is the simple name of the component type with "[]" appended. In particular the simple name of an array whose component type is anonymous is "[]".

Returns:
the simple name of the underlying class


Examples


package com.logicbig.example.clazz;

public class GetSimpleNameExample {

public static void main(String... args) {
Class<GetSimpleNameExample> c = GetSimpleNameExample.class;
String simpleName = c.getSimpleName();
System.out.println(simpleName);
}
}

Output

GetSimpleNameExample




package com.logicbig.example.clazz;

import java.io.Serializable;
import java.lang.reflect.Array;

public class GetSimpleNameExample2 {

public static void main(String... args) {
System.out.println("-- Anonymous class --");
Class<? extends Serializable> c = new Serializable() {}.getClass();
System.out.println("Simple name: " + c.getSimpleName());

System.out.println("-- Arrays of Anonymous class --");
Class<? extends Serializable[]> c2 = new Serializable[]{new Serializable() {}}.getClass();
System.out.println("Simple name: " + c2.getSimpleName());

System.out.println("-- Arrays of Anonymous class created via reflection --");
Class<?> c3 = Array.newInstance(new Serializable() {}.getClass(), 1).getClass();
System.out.println("Simple name: " + c3.getSimpleName());
}
}

Output

-- Anonymous class --
Simple name:
-- Arrays of Anonymous class --
Simple name: Serializable[]
-- Arrays of Anonymous class created via reflection --
Simple name: []




package com.logicbig.example.clazz;

import java.io.Serializable;

public class GetSimpleNameExample3 {

public static void main(String... args) {
Class<int[]> c = int[].class;
System.out.println(c.getSimpleName());
}
}

Output

int[]




See Also