Java - How to get class name

By xngo on June 19, 2019

Here are ways to get the class name.

package net.xngo.tutorial.java.lang;
 
public class GetClassName
{
    public static void main(String[] args)
    {
        // Output fully qualified class name: net.xngo.tutorial.lang.GetClassName
        System.out.println(new GetClassName().getClass().getName());
        System.out.println(GetClassName.class.getName());
        System.out.println(GetClassName.class.getCanonicalName());
 
        // Output class name: GetClassName
        System.out.println(new GetClassName().getClass().getSimpleName());
        System.out.println(GetClassName.class.getSimpleName()); 
    }
}

Ouput

net.xngo.tutorial.java.lang.GetClassName
net.xngo.tutorial.java.lang.GetClassName
net.xngo.tutorial.java.lang.GetClassName
GetClassName
GetClassName

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.