BALS BALS - 9 months ago 28
Java Question

What happens during Java object initialization

After I read many article in internet.I assumed object initialization is actually allocating object memory in heap but I couldn't be sure about it .
Please anyone explain me what happens behind the scene of object initialization or give me an reference link.

Answer Source

Java doesn't actually allocate space until a 'new' object is created. Declaring a reference for an object does not allocate memory on the heap.

When a first instance of a new object is created - the initialization of the class / interface (static members, etc.) happens in the order provided in the Java Specification (See section 12.4) https://docs.oracle.com/javase/specs/jls/se8/jls8.pdf

Then the instance of the object initialization things happen as referenced in Section 12.5 of the same document.