Junior — Middle
Каким образом реализован вызов метода getClass в Java и что он возвращает?
sobes.tech AI
Answer from AI
The getClass() method in Java is defined in the Object class and is available to all objects. It is called without parameters and returns an object of type Class<?>, which represents the class of the specific object at runtime.
The implementation of getClass() is built into the JVM and directly returns the metadata of the object's class. This allows obtaining information about the object's type, such as the class name, methods, fields, etc.
Example usage:
String s = "Hello";
Class<?> clazz = s.getClass();
System.out.println(clazz.getName()); // Outputs: java.lang.String
Thus, getClass() allows dynamically determining the type of an object at runtime.