public class B bracket public static B t1 = new B(); public static B t2 = new B(); bracket System.out.println ("building block"); bracket static bracket System.out.println ("static block"); bracket public static void main(String args) bracket B t = new B(); bracket bracket
When initializing t1 and t2, two B-type objects will be constructed respectively, and the construction block will be called when creating the object. This explains the first 2 lines of output.
When all static variables are initialized, the static block is executed, and the static block is executed only once.
When the main method starts to run, the object T of type B is constructed again, then the construction block is executed again, several objects are constructed, and the construction block is executed several times.
You can use javap -c to view bytecode and find that static blocks and static attributes of classes are essentially the same, and the execution order of static blocks and static attributes is the order in which the code is written.