Java Learning static blocks, display initialization blocks, constructors, this call in constructors, difference between contact and call order

Source: Internet
Author: User

1. Static code blocks are---initialized to the class, executed only once when the class is loaded (note: Only a reference to the class is created (that is, only the declaration) does not touch the load on that class)

/**      * Static code block     * Features: Executes as the class is loaded, executes only once, and is used to     initialize the class by rows */     static{        System.out.println ("a");    }

2. (Display initialization block) constructs a block of code---initializes the object,

/**      * Construct blocks of code     * Function: Initialize objects     * Objects are run as soon as they are established, and      the difference between the constructor execution * * and the constructor is that the     Construction code block is a unified initialization of all objects, Instead, the constructor initializes the corresponding object (for different objects to be initialized by rows, such as the parameterless constructor, the parameter constructor)     *      * Constructs a code block that defines the initialization content       of different object commonalities */    {            }

3. Constructor--class to the corresponding object

4.this

/*** This statement, used to call each other between constructors, because this represents the corresponding object, and the constructor is to initialize the corresponding object, all the same object * This statement, can only be defined in the first line of the constructor-the interpretation of the rule: because the initialization is performed first, the value of its own set after Polygons can override the initialization of the system, or the default initialization of the system overrides its own settings * *@paramname*/     PublicDemo (String name) { This. name= "Haha"; }     PublicDemo (String name,intAge ) {         This(name);  This. name=name;  This. age=Age ; System.out.println ("A:name=" +name+ ", age=" +Age );//cry ();}

The following content total code:

 Packagecom.dreamly.day01;/** * @authordreamly **/ Public classDemo {PrivateString name; Private intAge ;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }        /*** Static code block * Features: Executes as the class is loaded, executes only once, and is used to initialize the class through rows*/    Static{System.out.println (A); }    /*** Construct blocks of code * function: Initialize objects * objects are run as soon as they are established, and the difference between the constructor execution * * and the constructor is that the constructor code blocks are uniformly initialized for all objects, and the constructors give the corresponding objects initial Initialization (for different objects to be initialized by rows, such as parameterless constructors, parametric constructors) * * Constructs a block of code that defines what is initialized for different object commonalities*/    {        //The initialization section that defines the commonality of different objects can be written here, such as the Cry () methodcry (); }         PublicDemo () { This("Hah"); System.out.println ("A:name=" +name+ ", age=" +Age );//cry ();    }    /*** This statement, used to call each other between constructors, because this represents the corresponding object, and the constructor is initialized to the corresponding object, all the same object * This statement, can only be defined in the first row of the constructor-the interpretation of the rule: because the initialization is executed first, the value of its own set in the back To override the initialization of the system, or the default initialization of the system overrides its own settings * *@paramname*/     PublicDemo (String name) { This. name= "Haha"; }     PublicDemo (String name,intAge ) {         This(name);  This. name=name;  This. age=Age ; System.out.println ("A:name=" +name+ ", age=" +Age );//cry ();    }            Private voidcry () {System.out.println ("Cry ..."); }}
 Package com.dreamly.day01; /**  @author*/Publicclass  demotest    {public  staticvoid  main (string[] args) {        demo d=New  demo ();        D.setname ("Lily");        D.setage ();}    }
 Packagecom.dreamly.day01;/** * @authordreamly **/ Public classDemo2 {intNum=5; Static{System.out.println ("B"); } {System.out.println ("C" + This. Num); } Demo2 () {System.out.println (A); } Demo2 (intnum) {System.out.println ("D"); }         Public Static voidMain (string[] args) {Demo2 D2=NewDemo2 (); D2.num= 4;//execution Order b c5 a    }        }
 Packagecom.nico.test; Public classDemo1 {Static inti; Static {//System.out.println ("static Block");i = 8;//2} {System.out.println ("Initialize block"); I= 10;//7    }         PublicDemo1 () {//If there is an explicit initialization block, the initialization block is performed first 4         This("i =" + i);//5 If you want to call other constructors in a parameterless construct, then this statement can only be written in the first lineSYSTEM.OUT.PRINTLN ("No parameter constructor"); }         PublicDemo1 (String str) {//6System.out.println (str);//8 "i = 8";    }     Public Static voidMain (string[] args) {Demo1 d=NewDemo1 ();//1 3    }}

Java Learning static blocks, display initialization blocks, constructors, this call in constructors, difference between contact and call order

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.