getClass method in java
How is it that the getC开发者_如何学Pythonlass method in Object class is capable of dynamically returning Class?
It doesn't return the class name - it returns the Class
representing 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.
And there's nothing dynamic about it. The class of the object can never change.
精彩评论