GetClass method in java

How is it that the getClass method in the Object class is able to dynamically return the class?

+3
source share
2 answers

It does not return the name of the class - it returns Classrepresenting this type of object. Each object “knows” what type it really is — how castes can work or fail depending on the type of runtime. Object.getClass()just retrieves the relevant information from the object.

If you have a class that contains only one int, each object will still occupy more than 4 bytes in memory: there actually is a "header" of the object containing things such as a link to the actual type of the object, information to do with the monitor associated with this object, etc.

+11
source

And there is nothing dynamic about it. The class of an object can never change.

+1
source

Source: https://habr.com/ru/post/1744973/


All Articles