Title is the conclusion that if someone asks you the Java class member initialization order and initialization block knowledge then answer him. Here's the code:
Packagecom.test; Public classtestclass{//member variable assignment the first execution of the Private Static intM1=1; Private intM2=1; //Static constructors A second execution Static{System.out.println ("Member variable assignment precedes static constructor m1=" +M1); M1=2; } //The third execution of the constructor function PublicTestClass () {System.out.println ("Static constructors precede constructor m1=" +M1); M1=3; System.out.println ("Member variable assignment precedes constructor m2=" +m2); M2=3; } Public voidprintm1m2 () {System.out.println ("M1=" +M1); System.out.println ("M2=" +m2); } Public Static voidMain (string[] args) {TestClass T=NewTestClass (); T.PRINTM1M2 (); }}
Console output:
The member variable is assigned before the static constructor m1=1 the static constructor is assigned a value before the constructor M1=2 member variable before the constructor m2=1M1 =3m2 =3
In a Java class, a member variable is assigned the first, followed by a static constructor, again a constructor