What is Get Class In Java?
In Java, the getClass()
method is a protected method defined in the Object
class. When called on an object, it returns the runtime class of that object as an instance of the Class
class. This method is useful for obtaining information about the object's type, such as its name, methods, and fields. It is often used in reflection and debugging to identify the actual class of an object, especially when dealing with polymorphism. Here's an example:
Object obj = new String("Hello");
Class<?> clazz = obj.getClass(); // clazz holds the String class information