Remember the order of class initialization

        I have been writing java for some years, and the folds on my face have grown from nothing, and the hair on my head has grown from nothing to nothing. Thinking about it now, it is really as bitter as the sea of ​​​​code.

What I want to talk about today is about the order of class initialization, that is, the instantiation operation that everyone often talks about. Although it is rarely used in ordinary work, it is still very helpful for reflection. My colleagues and I summarized this order into three sentences for convenience.

  1. Static first, then non-static
  2. First parent class, then child class
  3. Others are executed sequentially in code order

ps: The above are all executed in the order of 1 to 3

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326136577&siteId=291194637