Reference: http://uule.iteye.com/blog/1558891
Static code blocks in Java are executed when the virtual machine loads the class, and only once. If there are multiple static blocks of code, the JVM executes them sequentially in the order in which they appear in the class, and each code block is executed only once.
A non-static block of code is executed at the time of the class new instance, and every time a new object instance executes.
Order of execution of code
- Static code blocks for the keynote class
- Static code block for object parent class
- Static code blocks for objects
- Non-static code block for object parent class
- Constructor for object parent class
- Non-static block of code for an object
- The constructor of the object
Sample code
PublicClass StaticBlockTest1 {Non-static code block {System) of the keynote class.Out.println ("StaticBlockTest1 not static Block"); }Static code blocks for the keynote classstatic {System.Out.println ("StaticBlockTest1 static Block"); }PublicStaticBlockTest1 () {System.Out.println ("Constructor StaticBlockTest1"); }PublicStaticvoidMain (string[] args) {Children children =new Children (); Children.getvalue (); }}class parent{private String name;Privateint age;Parent class No parameter construction methodPublicParent () {System.Out.println ("Parent constructor method"); {System.Out.println ("Parent constructor method--> not static block"); } }The non-static code block {System) of the parent class.Out.println ("Parent not static Block"); Name ="Zhangsan"; Age =50; }Parent class static code blockstatic {System.Out.println ("Parent static Block"); }Parent class has a method for constructing a parameterPublicParent (String name,int age) {System.Out.println ("Parent constructor method");THIS.name ="Lishi";This.age =51; }Public StringGetName () {return name; }PublicvoidSetName (String name) {THIS.name = name; }PublicIntGetage () {return age; }PublicvoidSetage (int age) {This.age = age; }}class Children extends parent{Sub-class static code blockstatic {System. Out.println ("Children static Block");} //Sub-class non-static code block {System. Out.println ("Children not static Block");} //Subclass No parameter construction method public children () {System. Out.println ("Children constructor Method");} public void GetValue () { //this.setname ("Lisi"); //this.setage (51); System. out.println (this.getname () + this.getage ());}}
Output results
StaticBlockTest1StaticBlockStatic code block for the keynote class, parentStaticBlockStatic code block for parent class childrenStaticBlock//Subclass parent not static Span class= "Hljs-keyword" >block //non-static code block of parent class parent constructor method//Parent class constructor Parent constructor method--> not static blockchildren not Span class= "Hljs-title" >static block//The non-static code block of the keynote class children constructor method//The constructor of the keynote class ZHANGSAN50//Keynote main method
Summarize:
- Execute in the order mentioned above
- The subclass defines the method of non-parametric construction, and the calling parent class is also a non-parametric construction method
(RPM) Java static code blocks and non-static code blocks