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

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();
}
Have Questions? Post them here!
- How to Get List of All Country Codes in Java Using Locale Class
- Unsupported major.minor version 52.0 in java
- Java - How to set custom thread name?
- Get the current timestamp in Java
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- java: unclosed string literal [Error]
- Convert Java Byte Array to String with code examples
- Error: Can not find the tag library descriptor for
- Java 8 - Convert List to Map Examples
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds
- Fix java.net.ProtocolException: Invalid HTTP method
- Java: Convert Stream to List
- Java equals method - Tutorial
- List of Java JDBC Database Driver Jars, Classes and URLs Details
- Read YAML file Java Jackson Library
- How to display Java Date Time timezone GMT/UTC offset using SimpleDateFormat
- List of Java Keywords
- Enable JSON Pretty Print in Java Jackson
- How to Word-Warp Console logs in IntelliJ
- Convert Map to List in Java 8 using Stream API
- Create a Directory using Java Code
- Ways to Convert Integer or int to Long in Java
- [Program] How to read three different values using Scanner in Java
- Java JDBC Example with Oracle Database Driver Connection
- Android M cannot run app using play button : Android Studio - Android
- Android : Execute some code after back button is pressed - Android
- Convert String Date to Date Object in Java - Java
- Open VS Code Command Palette using Keyboard Shortcut - Shortcuts
- Fix Microsoft Teams error We're sorry—we have run into an issue Try again - Teams
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds - Java
- Install Microsoft OneDrive on Mac - MacOS
- How to Adjust macOS System Font Size - MacOS