Inheritance and Object creation, Theoretically and in Real

前端 未结 3 1079
庸人自扰
庸人自扰 2020-12-16 06:44

Lets say I have a class A.java,

\"enter

When I will execute a con

3条回答
  •  醉话见心
    2020-12-16 07:14

    First, a tid bit... calling the constructor of an object does not allocate it. In bytecode, the initialization new Object() is expressed as something to the effect of...

    new java/lang/Object
    invokespecial java/lang/Object ()V
    

    The new instruction takes care of allocating the space and acquiring a reference to the yet uninitialized object, while the invokespecial handles calling the constructor itself (which is internally compiled to a void method named , hence the descriptor ()V).

    Moving on, internals of object allocation and representation on the heap are entirely JVM specific. However, as far as I know, there is only one allocated object per allocated object, no matter its number of super classes. The object itself in memory has space for the instance fields of both its own class and its super classes. It must also have space for a virtual method table, in order to do virtual dispatch when performing virtual method calls (e.g. via invokevirtual) for the object.

    Internally, the Oracle HotSpot JVM manages things called oops, or ordinary object pointers. You can read more about the HotSpot memory layout here. Feel free to browse the HotSpot source repository.

提交回复
热议问题