(RPM) Java static code blocks and non-static code blocks

Source: Internet
Author: User

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
    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
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             
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

Summarize:

    1. Execute in the order mentioned above
    2. 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

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.