YouTip LogoYouTip

Java Object Getclass

## Java Object getClass() Method The `getClass()` method of the `java.lang.Object` class is used to retrieve the runtime class of an object. It returns a `Class` object that represents the class of the object at runtime, which is a key entry point for Java Reflection. Since every class in Java implicitly inherits from `java.lang.Object`, the `getClass()` method is available on all Java objects (except for primitive types, which must be wrapped in their respective wrapper classes first). --- ### Syntax ```java public final Class getClass() ``` ### Parameters * **None** β€” This method does not accept any parameters. ### Return Value * It returns a **`Class`** object representing the runtime class of the object. This object is locked by the `static synchronized` methods of the represented class. --- ### Code Examples #### Example 1: Using `getClass()` with Standard Java Classes The following example demonstrates how to use the `getClass()` method with standard library classes like `Object`, `String`, and `ArrayList`. ```java import java.util.ArrayList; class GetClassExample { public static void main(String[] args) { // 1. Using getClass() with an Object instance Object obj1 = new Object(); System.out.println("The class of obj1 is: " + obj1.getClass()); // 2. Using getClass() with a String instance String obj2 = new String(); System.out.println("The class of obj2 is: " + obj2.getClass()); // 3. Using getClass() with an ArrayList instance ArrayList obj3 = new ArrayList<>(); System.out.println("The class of obj3 is: " + obj3.getClass()); } } ``` **Output:** ```text The class of obj1 is: class java.lang.Object The class of obj2 is: class java.lang.String // Note: Generics are erased at runtime, so it returns java.util.ArrayList The class of obj3 is: class java.util.ArrayList ``` --- #### Example 2: Using `getClass()` with Custom Classes You can also call `getClass()` on instances of your own custom classes. ```java class GetClassCustomExample { public static void main(String[] args) { // Create an instance of the custom class GetClassCustomExample obj = new GetClassCustomExample(); // Since GetClassCustomExample inherits from Object, we can call getClass() System.out.println("The class of obj is: " + obj.getClass()); } } ``` **Output:** ```text class GetClassCustomExample ``` --- ### Key Considerations and Advanced Usage #### 1. Runtime Class vs. Compile-time Type The `getClass()` method returns the actual **runtime** type of the object, not the declared compile-time reference type. This is particularly important when working with polymorphism and inheritance. ```java class Animal {} class Dog extends Animal {} public class Test { public static void main(String[] args) { Animal myDog = new Dog(); // Declared as Animal, but instantiated as Dog // Prints "class Dog", because the runtime object is a Dog System.out.println(myDog.getClass()); } } ``` #### 2. `getClass()` vs. `instanceof` * **`instanceof` operator:** Evaluates to `true` if the object is an instance of the specified class or any of its subclasses (is-a relationship). * **`getClass()` comparison:** Performs an exact type check. It will only match if the object is of the exact same class, excluding subclasses. ```java Animal myDog = new Dog(); System.out.println(myDog instanceof Animal); // true (polymorphic check) System.out.println(myDog.getClass() == Animal.class); // false (exact type check) System.out.println(myDog.getClass() == Dog.class); // true ``` #### 3. Retrieving Class Metadata Once you have obtained the `Class` object via `getClass()`, you can use Java Reflection to inspect the class structure, such as its name, methods, fields, and constructors: ```java String text = "Hello YouTip"; Class clazz = text.getClass(); System.out.println("Simple Name: " + clazz.getSimpleName()); // Output: String System.out.println("Canonical Name: " + clazz.getCanonicalName()); // Output: java.lang.String ```
← Java Object FinalizeJava Object Equals β†’