Static code block of Java

Source: Internet
Author: User
Tags validation examples

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:
    1. Static code blocks for the keynote class
    2. Static code block for object parent class
    3. Static code blocks for objects
    4. Non-static code block for object parent class
    5. Constructor for object parent class
    6. Non-static block of code for an object
    7. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.