Here is what JavaDoc says:
public final Class <?> getClass()
Returns the runtime class of this
Object
. The returnedClass
object is the object that is locked bystatic synchronized
methods of the represented class.
The actual result type isClass<? extends |X|>
where|X|
is the erasure of the static type of the expression on whichgetClass
is called. For example, no cast is required in this code fragment:Number n = 0; Class<? extends Number> c = n.getClass();
Returns:
The Class object that represents the runtime class of this object.
Now , I understand it is a native method , so it is is implemented in platform-dependent code. But what about the return type of this method.
public final Class<?> getClass()
Also , consider the code:
class Dog
{
@Override
public String toString()
{
return "cat";
}
}
public class Main
{
public static void main(String[] args)
{
Dog d= new Dog();
//Class<Dog> dd = new Dog(); Compile time error
System.out.println(d.getClass());
}
}
Output:
class Dog
So, my query lies in :
- Return type of this method
- toString method is not called . A similar post on this topic is : Java. getClass() returns a class, how come I can get a string too?
- The commented code which otherwise give compile time error.