All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Java Get Class Name

Last Updated : Mar 11, 2024

Java Get Class Name

In this article we will show you the solution of java get class name, Java's getClass() method returns a Class object that represents the object's runtime class, which you can then use to retrieve the current class names.

In addition to returning the fully qualified name of the class, the getName() method also returns the package name.

You can use the getSimpleName() method of the Class class if you want only the class' simple name (without the package).

We will now discuss the idea of how to get class name in java with an example.

Step By Step Guide On Java Get Class Name :-

Code 1

import java.util.ArrayList;
class Main {
  public static void main(String[] args) {
    // getClass() with Object
    Object obj1 = new Object();
    System.out.println("Class of obj1: " + obj1.getClass());
    // getClass() with String
    String obj2 = new String();
    System.out.println("Class of obj2: " + obj2.getClass());
    // getClass() with ArrayList
    ArrayList<Integer> obj3 = new ArrayList<>();
    System.out.println("Class of obj3: " + obj3.getClass());

Code 2

import java.lang.reflect.Field;
public class TalkersCode{
 int x;
int y;
public String getClassName() {
String className = this.getClass().getSimpleName();
System.out.println("Name:" + className);
return className;
public Field[] getAttributes() {
Field[] attributes = this.getClass().getDeclaredFields();
for(int i = 0; i < attributes.length; i++) {
System.out.println("Declared Fields" + attributes[i]);
return attributes;
public static void main(String args[]) {
Test t = new Test();
  1. We start by defining a class called "Main".
  2. The Main class contains our main method, which contains the code.
  3. This step involves creating an instance of Object class "obj1".
  4. We then use the getClass() method on the object instance "obj1" and print the output to the console.
  5. We repeat the process for an instance of the String class called "obj2".
  6. We then create an instance of the ArrayList class with a parameterized type of "Integer" called "obj3".
  7. Finally, we use the getClass() method on the ArrayList instance "obj3" and print the output to the console.
  8. We start off by declaring a public class named TalkersCode.
  9. We declare two integer variables x and y inside the class.
  10. We declare a public method named getClassName which returns a String.
  11. Inside the getClassName method, we use the getClass method to get the class object of the current instance and then use the getSimpleName method to get the name of the class in string format.
  12. We print the name of the class using the System.out.println method and then return the name.
  13. We declare another public method named getAttributes which returns an array of Field objects.
  14. Inside the getAttributes method, we use the getDeclaredFields method of the Class class to get an array of all the Field objects declared inside the class.
  15. We loop through the array of Field objects and print out each field using the System.out.println method.
  16. We finally return the array of Field objects.
  17. In the main method, we create an object of the Test class.
  18. We call the getClassName method on the object t and print out the name of the class.
  19. We call the getAttributes method on the object t and print out all the declared fields of the class.

Conclusion :-

As a result, we have successfully learned how to get class name in java with an example.

Java tutorials show you how to make classes initialize with class field initializers and class initialization blocks, and how to make objects initialize with constructors, object field initializers, and object initialization blocks.

I hope this article on java get class name helps you and the steps and method mentioned above are easy to follow and implement.