How to know the Class Name and Path of a Runtime Java Object?


At times you may want to know what is the name of the Class for a runtime Object in Java, in such case make use of the getClass() method from the java.lang.Object Class.

Let's take a look at some examples:

Example:

    Employee.java
    package org.example;
    
    public class Employee {
    
        private int employeeId;
        private String employeeName;
    
        public Employee(int employeeId, String employeeName) {
            this.employeeId = employeeId;
            this.employeeName = employeeName;
        }
    
        public int getEmployeeId() {
            return employeeId;
        }
    
        public void setEmployeeId(int employeeId) {
            this.employeeId = employeeId;
        }
    
        public String getEmployeeName() {
            return employeeName;
        }
    
        public void setEmployeeName(String employeeName) {
            this.employeeName = employeeName;
        }
    }
    Main.java
    1. package org.example;
    2. 
    3. public class Main {
    4. 
    5.    public static void main(String[] args) {
    6.
    7.        Employee emp = new Employee(1,"Mikey");
    8.        Class empClass = emp.getClass(); //get the class for the runtime object emp
    9.
    10.        System.out.println("Class Name: "+ empClass.getSimpleName());
    11.        System.out.println("Class Name as Classpath: " + empClass.getName());
    12.       System.out.println("Package Name: "+ empClass.getPackage());
    13.
    14.    }
    15. }
    Output:

    Employee
    org.example.Employee
    package org.example

    Output of Java Code to get the Class name and package for runtime object

    As you may see on line number 8, we have made use of the getClass() method from the Object class to return the runtime class of this Object emp.

    Next on line 10, we have made use of the reflection method getSimpleName() just to print the name of the Class (note: this does work for class, interface, array class, primitive type, or void)

    On line 11, getName() returns the Class name and the path of the class (classpath) as a response String.

    On line 12, getPackage() returns the Package name of the class Employee.



Let us take a look at some more examples:

    package org.example;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Main {
    
        public static void main(String[] args) {
    
            List<String> countries = new ArrayList<>();
            countries.add("USA");
            countries.add("Canada");
            countries.add("France");
            countries.add("Sweden");
    
            System.out.println(countries.getClass());
            System.out.println(countries.get(2).getClass());
    
        }
    }
    
    Output:

    class java.util.ArrayList
    class java.lang.String

    Note: The Class.java class has toString() method overridden so you can simply use getClass to print out the class/interface name.

    toString method implementation of Class.java file:
        public String toString() {
            return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
                + getName();
        }

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap