Difference between initializing a class and instantiating an object?

后端 未结 4 1666
鱼传尺愫
鱼传尺愫 2020-11-29 05:46

I tried searching for this question through the search engine but could find a topic that explained the difference between initializing a class and instantiating an object.<

4条回答
  •  抹茶落季
    2020-11-29 05:46

    When a Java class is "loaded" into the JVM the class representation must be initialized in several ways.

    • The class's "constant pool" is expanded into a runtime structure and some values in it are initialized.
    • The superclass of the class is located (via the constant pool) and attributes of it extracted.
    • A method table is constructed for the methods of the class. The individual methods are marked as "not yet verified".
    • Several verification operations are performed on the class representation.
    • Static fields are initialized.
    • On first reference, string literals are "interned" and the interned string pointer is placed in the constant pool
    • On first reference methods are "verified".
    • Et al.

    There is a specific set of terminology used to refer to class initialization, though I don't recall the specifics. Certain things can only occur after a class has been initialized to a specific point, etc.

    Instantiating an object can only occur after the class has been loaded and initialized (though all methods do not need to have been verified). The size of the object is gotten from the class and that much heap is located and zeroed. The object header is filled in with a pointer to the class and other fields used to manage the class. Then the appropriate constructor method for the class is invoked (and it will invoke any super's constructor).

提交回复
热议问题