how to get class name in java code example

Example 1: java class name to string

String className = n.getClass().getName();
String name = className.substring(className.lastIndexOf('$')+1,className.length()-4);
System.out.println(name);

Example 2: getting class name in java

a.getClass().getName();

Example 3: java get classname

//Just the name of the class
myObject.getClass().getSimpleName();

//The name of the class with the package name
myObject.getClass().getName();

Example 4: java get class name of object

myObject.getClass().getName()

Example 5: how to use getclass in java

public static String printClassInformation(Object o){
	Class cl=o.getClass();
  	System.out.println("Name: "+cl.getSimpleName());
  	System.out.println("Full name: "+cl.getCanonicalName());
  	System.out.println("Methods: "+Arrays.toString(cl.getMethods()));
}

Tags:

Misc Example