Java Object getClass() MethodLast Updated : 13 Jan 2026 The getClass() method in Java is a fundamental method inherited from the Object class, which is the root of the Java class hierarchy. It allows us to retrieve the runtime class of an object. Every class in Java directly or indirectly inherits from this class. getClass() is the method of Object class. The class object which is returned is the object that is locked by static synchronized method of the represented class. PurposeThe getClass() method is used to obtain the runtime class of an object. It returns an instance of the Class class, which provides methods to inspect the properties of the class, such as its name, superclass, interfaces, constructors, methods, and fields. SyntaxReturnsIt returns the Class objects that represent the runtime class of this object. ExampleExample 1: JavaObjectgetClassExample1 Output: Class of Object obj is : java.lang.String Example 2: JavaObjectgetClassExample2.java Output: Class of Object obj is : java.lang.String Exploring the Depths of the getClass() Method in JavaUnderstanding Inheritance, Polymorphism, Reflection, and the Java Class Hierarchy Inheritance: From the Object class, which is the root of all the Java classes, every Java object inherits the getClass() method. It implies that it will be possible to getClass() for any object in Java. Polymorphism: The method getClass() is polymorphic. It means it returns the real runtime class of the object and not the reference type. It enables us to perform dynamic method invocation and runtime type checking. Reflection: The getClass() method is a key component of the namespace of Java's reflection API. Reflection enables the examination and changing of classes, methods, and fields by manipulating them at runtime. With the getClass() method, we can inspect the dynamic object and interact with its structure. Class Hierarchy: The Class class represents classes and interfaces to a Java application. It is a part of the java.lang package. The class Class provides the necessary methods to explore the characteristics of a class, including its name, superclass, interfaces, constructors, methods, and fields. GetClassExample.java Output: Class of animal: Animal Class of dog: Dog Class of cat: Cat Class of dogAnimal: Dog Class of catAnimal: Cat Class of animal: Animal Applications of getClass() Method
GetClassExample1.java Output: The animal is a Dog. Class of dog: Dog Deserialized object is of Dog class. Issues with getClass() MethodInappropriate Use of getClass() with GenericsGenerics in Java are implemented using type erasure, meaning that type parameters are not available at runtime. When we call getClass() on a generic type, it returns the runtime class of the raw type, not the generic type itself. It can lead to unexpected behavior or confusion when dealing with generic collections. Scenario: Suppose we have a List<String> and you call getClass() on it. Instead of getting List<String>, we get ArrayList, the raw type. It can be misleading, especially if you're expecting to work with the generic type. Incorrect Handling of Array TypesWhen you call getClass() on an array object, it returns a different result compared to calling it on regular objects. Instead of returning the runtime class of the array elements, it returns an internal class representation indicating that it's an array. Scenario: If you have an array of integers (int[]) and you call getClass() on it, we will get a class representing arrays of integers (I) that might not be intuitive for someone expecting to get int.class. Unexpected Behavior with Primitive Types Vs.Wrapper ClassesWhen we call getClass() on a primitive type, such as int, it returns the corresponding wrapper class, such as java.lang.Integer. The behavior might be unexpected if we are expecting to get the primitive type itself. Scenario: If we have an int variable and you call getClass() on it, we will get java.lang.Integer, which might be surprising if we are expecting to get int.class. Limited Usefulness in Some ScenariosIn certain scenarios, the getClass() method might not provide sufficient information for certain operations. For example, it might not be suitable for distinguishing between different subclasses of a superclass at runtime. Scenario: Suppose we have a method that receives an object of a superclass type, and you want to perform different actions based on the actual subclass type. Using getClass() might not be sufficient for distinguishing between subclasses in all cases. Performance Overhead with Reflection:Using getClass() along with reflection can incur performance overhead, especially in performance-sensitive applications. Reflection involves introspecting classes, methods, and fields at runtime that can be resource-intensive. Scenario: If we are using getClass() extensively in a performance-critical section of your code, it might lead to noticeable slowdowns due to the additional overhead introduced by reflection operations. Not Suitable for Type-Safe Comparisons:Using the getClass() method for type-safe comparisons can be error-prone because it compares the runtime class of objects, not their types. It can lead to unexpected behavior if subclasses are involved. Scenario: If we are trying to perform type-safe comparisons between objects of different subclasses, using the getClass() method might not produce the expected results, as it compares the actual runtime classes, not the declared types. GetClassDemo.java Output: Issue 1: Incorrect Handling of Array Types Original Class of array: [I Expected: [I (representing array of integers) The animal is not a Dog. Issue 2: Limited Usefulness in Some Scenarios Original: The animal is not a Dog. Expected: The animal is a Dog. Issue 3: Performance Overhead with Reflection Original: No output, but using getClass() with reflection can incur performance overhead Expected: No performance overhead incurred The animal is not a Dog. Issue 4: Not Suitable for Type-Safe Comparisons Original: The animal is not a Dog. Expected: The animal is a Dog. Next TopicInheritance(IS-A) |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India