开发者

getClass method in java

开发者 https://www.devze.com 2022-12-29 18:52 出处:网络
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 typ

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消