What is the order of inheritance loading in Java?

The class loading order in Java follows the following sequence:

  1. Load the superclass: First load the superclass, including its static members and static code blocks, before loading the subclass.
  2. Load subclass: Load the static members and static code blocks of the subclass, but do not execute the subclass’s constructor.
  3. Object Creation: The constructor methods of the parent class and the subclass are executed in order of object creation. Firstly, the parent class constructor method is executed, followed by the subclass constructor method.

It is important to note that although the order of loading and creating objects follows the above sequence, in the actual code execution process, there may be other factors that lead to a change in order. For example, if an object is created in a static code block, the order of object creation may be different from the above order. Additionally, features such as threads or reflection may also cause changes in the order of loading and creating objects. Therefore, when writing code, it is advisable to avoid relying on the order of loading and creation.

bannerAds