In what order do static/instance initializer blocks in Java run?

前端 未结 8 1660
心在旅途
心在旅途 2020-11-22 17:01

Say a project contains several classes, each of which has a static initializer block. In what order do those blocks run? I know that within a class, such blocks are run in

8条回答
  •  野性不改
    2020-11-22 17:41

    You can have multiple static and instance initializers in the same class, therefore

    • Static initializers are called in the textual order they are declared (from 12.4.2)
    • Instance initializers are called in the textual order they are declared (from 12.5)

    Each is executed as if it was a single block.

提交回复
热议问题