In some cases, some code needs to be executed when the project is started, and a static block of code is required, and this code is executed proactively.
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.
The order in which the code is executed:
- 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
Static code block
static{
...;
}
Static code block
{
...;
}
code example:
1 PackageStaticblock;2 3 Public classStaticblocktest {4 //non-static code blocks for the keynote class5 {6System.out.println ("Staticblocktest not static block");7 }8 9 //Static code blocks for the keynote classTen Static { OneSystem.out.println ("Staticblocktest static Block"); A } - - Publicstaticblocktest () { theSystem.out.println ("Constructor Staticblocktest"); - } - - Public Static voidMain (string[] args) { + //Static code blocks are only executed before static methods are executed - Children.say (); +System.out.println ("----------------------"); A at //All Validation Examples -Children children =Newchildren (); - Children.getvalue (); - } - } - in /** - * Parent Class to */ + classParent { - the PrivateString name; * Private intAge ; $ Panax Notoginseng //parent Class No parameter construction method - PublicParent () { theSystem.out.println ("Parent constructor Method"); + { ASystem.out.println ("Parent constructor method--> not static block"); the } + } - $ //non-static block of code for the parent class $ { -System.out.println ("Parent not static block"); -Name = "Zhangsan"; theAge = 50; - }Wuyi the //Parent class static code block - Static { WuSystem.out.println ("Parent static Block"); - } About $ //Parent class has a method for constructing a parameter - PublicParent (String name,intAge ) { -System.out.println ("Parent constructor Method"); - This. Name = "Lishi"; A This. Age = 51; + } the - PublicString GetName () { $ returnname; the } the the Public voidsetName (String name) { the This. Name =name; - } in the Public intGetage () { the returnAge ; About } the the Public voidSetage (intAge ) { the This. Age =Age ; + } - } the Bayi /** the * Sub-class the */ - classChildrenextendsParent { - //sub-class static code block the Static { theSystem.out.println ("Children static Block"); the } the - //sub-class non-static code block the { theSystem.out.println ("Children not static block"); the }94 the //The method of sub-class non-parameter construction the PublicChildren () { theSystem.out.println ("Children constructor Method");98 } About - 101 Public voidGetValue () {102 //this.setname ("Lisi");103 //this.setage (Wuyi);104System.out.println ( This. GetName () + "" + This. Getage ()); the }106 107 Public Static voidsay () {108System.out.println ("Subclass static method Execution! ");109 } the}
Execution Result:
Staticblocktest static method Execution of the statically blockparent statics blockchildren statics block Subclass! ----------------------Parent not static Blockparent constructor Methodparent constructor method--> not static Blockchildren not static Blockchildren constructor Methodzhangsan 50
Details: http://uule.iteye.com/blog/1558891
Static code block of Java