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:

    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;
    1. package org.example;
    3. public class Main {
    5.    public static void main(String[] args) {
    7.        Employee emp = new Employee(1,"Mikey");
    8.        Class empClass = emp.getClass(); //get the class for the runtime object emp
    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());
    14.    }
    15. }

    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<>();

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

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

    toString method implementation of 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:

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