The class Object contains the following method:
public final Class<? extends Object> getClass().
why the return type of this method is Class<? extends Object>
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because Java lacks self types. (If it had them, then the return type would be
Class<? extends self_type>). So the signature merely declaresClass<?>, the (next) best it can do, which is less than ideal – the compiler certainly knows thatgetClass()doesn’t return any class, but a class that is a subtype of the static type of the expression on whichgetClass()is invoked.Thus we have this singularity, where the signature of the method declares
Class<?>because of limitations of the language, but the compiler treats the return value asClass<? extends TheClass>because it knows it is safe and wants to be helpful 🙂PS: you saw
Class<? extends Object>because you calledgetClass()on an expression with the static type ofObject.