Java - getClass() method

What is getClass() method

The getClass() method of the Object class returns the reference of the Class object.

Example

The following code gets the reference of the Class object for a Cat object:

Cat c = new Cat();
Class catClass = c.getClass();

The Class class is generic and its formal type parameter is the name of the class that is represented by its object.

You can rewrite the above statement using generics.

Class<Cat> catClass = (Class<Cat>)c.getClass();

Demo

public class Main{
  public static void main(String[] argv){
    Cat c = new Cat();
    Class<Cat> catClass = (Class<Cat>) c.getClass(); 
    String fullName = catClass.getName();
    String simpleName = catClass.getSimpleName();
    System.out.println(simpleName);
  }/*from  w w w  . j ava2 s  .  c o m*/
}
class Cat{
  
}

Result