Java program class name

Get Class Name in Java

Get Class Name in Java

  1. Get Class Name Using class.getSimpleName() in Java
  2. Get Class Name of an Anonymous Class Using getName()
  3. Get Name of Class Using Thread.currentThread().getStackTrace()[1].getClassName()

This tutorial teaches how to get the class name in Java using four methods. There are many cases where we may want to get the class name in Java.

Get Class Name Using class.getSimpleName() in Java

This is the most used way to get a class’s name. In the following example, we have two classes: GetClassName with the main() method, and another class is ExampleClass .

In the GetClassName class, we use ExampleClass.class to get the information of the class. It returns a Class instance of type ExampleClass . Now we can call getSimpleName() using the classNameInstance that will return only the class name as a String.

public class GetClassName   public static void main(String[] args)    ClassExampleClass> classNameInstance = ExampleClass.class;  String className = classNameInstance.getSimpleName();  System.out.println(className);   > >  class ExampleClass   private void exampleFun()   System.out.println("Just a function in a class");  > > 

Get Class Name of an Anonymous Class Using getName()

An inner class without any name is called an Anonymous class. In this example, we will learn how to get a class name if it is anonymous or check if a class has an anonymous class. Below, we create an interface Example to instantiate the anonymous class. In GetClassName , we instantiate the class without a name using the interface and implement the function printMessage() .

In order to get the class name, we use example.getClass() , but as there is no name of the class, we get an instance of type Class . Then we use classNameInstace to call getName() that returns the name of the class. As the output shows, we get the parent class name GetClassName with $1 appended, representing the anonymous class.

interface Example   void printMessage(); >  public class GetClassName   public static void main(String[] args)    Example example = new Example()   @Override  public void printMessage()    System.out.println("This is an anonymous inner class");  >  >;   Class classNameInstance = example.getClass();   String className = classNameInstance.getName();   System.out.println(className);   > > 

Get Name of Class Using Thread.currentThread().getStackTrace()[1].getClassName()

In the last example, we use the Thread class to get the current running thread using currentThread() that gives us access to the stack dump and all the invocations in the thread. getStackTrace() returns an array of stack elements from which we get the second item of the array and call getClassName() to get the class name of the invocation.

public class GetClassName   public static void main(String[] args)    String simpleClassName = Thread.currentThread().getStackTrace()[1].getClassName();   System.out.println("Class name: " + simpleClassName);   > > 

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

Related Article — Java Class

Copyright © 2023. All right reserved

Источник

Читайте также:  Php mysql query fetch all
Оцените статью