Describe the process of creating an instance of a class

Describe the process of creating an instance of a class
First, the class and its ancestor chain must be loaded, from the top down. We will discuss the ClassLoader and the class loading process in future posts. It's important to note that a class is loaded only once, at its first access within a given class loader.

After each class is loaded, memory is allocated for its static fields, and static initialization blocks are executed. In the future, not only the loading of the entire class but also the initialization of its static final fields might be lazy.

Next, the instance itself is instantiated. As with class loading, the process is carried out for the entire inheritance chain, starting with the furthest ancestor:
1. Memory is allocated in the heap for the instance, and a reference to this instance is obtained;
2. Initializations of non-static fields and initialization blocks are performed in the order they are declared;
3. The constructor is called;

Static fields of interfaces are not initialized when an object is created, and interfaces have no other state. This eliminates the question of ancestor initialization order in multiple inheritance.

During the object construction process, the issue of a virtual call in the constructor can arise, which is common in many languages. Effective Java Item 17 recommends not using overridable methods in an extendable class. An illustration of unexpected behavior resulting from this is provided below: