- Three major members of a class: member variable, constructor method, method, initialization code block is the 4th member of the class
- Initialization blocks are used to initialize classes or objects.
- The initialization block of a class can have more than 0~, executed sequentially
- As with the instance method, the class method, the instance variable, and the class variable, you can also initialize the block with the static modifier----statically initialize the block
- Initialization blocks are always executed before the constructor
- Non-static initialization block
- A non-static initialization block is equivalent to a constructor supplement that is used to initialize an object when it is created, before the constructor executes
- If a piece of initialization code is exactly the same for all constructors and does not need to receive parameters, it can be extracted into a non-static initialization code block
- In inheritance, a non-static block, a constructor for the parent class A is executed, a non-static block of the parent class B is executed, the constructor is executed, and the non-static block of the subclass, the constructor
- In fact, after compilation, non-static blocks have been added to the constructor and are located in front of all constructor code
- Static initialization blocks the execution order of static blocks, non-static blocks, construction methods in inheritance is shown by a piece of code
- Static initialization block with static modifier, also called class initialization block
- A non-static block is responsible for initializing the object, and the class initialization block is responsible for initializing the class, so the class initialization block is executed at the class initialization stage
- Static blocks, like static methods, cannot access non-static members
- In inheritance, the static block of the parent class A is executed successively, the static block of the parent class B, the static block of the last subclass, and then the non-static block and constructor of the parent class A, then the Class B, and finally the non-static block and constructor of the subclass.
- Because static blocks are done during the initialization phase of a class, when you create a second object of a class, the static blocks of that class do not execute
PublicClass test{Public Static void Main(string[] args) {C c1=NewC (); System.Out.println"--------the second time to create a Class C object---------"); C c2=NewC (); }}Class CExtends b{static {System.Out.println"C's static code block"); } {System.Out.println"C's non-static code block"); }C () {System.Out.println"Non-parametric construction method of C"); }C (String str) {System.Out.println"C's Constructive Method"); }}Class BExtends a{static {System.Out.println"B's static code block"); } {System.Out.println"B's non-static code block"); }B () {System.Out.println"Non-parametric construction method of B"); }B (String str) {System.out. println ( "B");}} class a{static {system.println ( "a static code block");} {System. out. println ( "a non-static code block");} a () {system.println ( "a" method of non-parametric construction);} a (String str) {system.println ( "a");}}
The output is:
a static block of code
static code block for B
Static code block for C
a non-static block of code
a method of non-parametric construction
non-static block of code for B
a method of non-parametric construction of B
non-static code block for C
the method of non-parametric construction of C
--------The second time you create a Class C object---------
a non-static block of code
a method of non-parametric construction
non-static block of code for B
a method of non-parametric construction of B
non-static code block for C
the method of non-parametric construction of C
execution order of static initialization blocks and static member variables
- When the JVM first uses a class, it allocates memory to all static members during the preparation phase, initializes these static member variables during the initialization phase, which is the initial value that is specified when the initialization code is executed or the member variable is declared, and the order of execution is the same as in the source code
- Follow the order in the source code, as shown in the sample code:
PublicClass test{Public Static void main (string[] args) {A a= New a (); System. out. println (A.num); //output 10 instead of 6, stating that the declaration assignment is 6 before performing a static block assignment of 10}} class a{public static int num=6; static{num=10;}
Java code block