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();
}
Facing issues? Have Questions? Post them here! I am happy to answer!
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
- Java 8 Streams map() with examples - Java
- How to Measure Execution Time in Jupyter Notebook Cell - Python
- How to download and install macOS 13 Ventura - MacOS
- Fix: incompatible types: Object cannot be converted to Integer - Java
- Restore deleted Office 365 SharePoint group site - SharePoint
- Compare two text files in Notepad++ - NotepadPlusPlus
- Calculate Area of ellipse - C-Program
- Ping IP/Server Address using Python Example - Python