Java Object getClass Method Example

In this example, we will demonstrate the usage of java.lang.Object.getClass() method with an example.

Object getClass() Method Example

The java.lang.Object.getClass() method returns the runtime class of this Object.

In this example, we first create a Person class and we will use getClass() method to print runtime Person class name.
class Person {
    private String firstName;
    private String lastName;

    public String getFirstName() {
        return firstName;

    public void setFirstName(String firstName) {
        this.firstName = firstName;

    public String getLastName() {
        return lastName;

    public void setLastName(String lastName) {
        this.lastName = lastName;
Use the main() method to demonstrate the usage of the getClass() method to print runtime Person class name:
public class Driver{
    public static void main(String[] args) {
        Person person = new Person();
Let us compile and run the above program, this will produce the following result −
class Person

Free Spring Boot Tutorial - 5 Hours Full Course

Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course