How is it that the getClass method in Object class is capable of dynamically returning Class?
Share
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.
It doesn’t return the class name – it returns the
Classrepresenting that object’s type. Each object “knows” what type it really is – that’s how casts can work or fail depending on the execution-time type.Object.getClass()just retrieves the relevant information from the object.If you have a class which only contains a single
int, each object will still take up more than 4 bytes in memory: there’s effectively an object “header” containing things like a reference to the object’s actual type, information to do with the monitor associated with that object etc.