1 /*2 To construct a code block:3 4 The purpose of constructing a code block is to initialize the object in a uniform format. 5 6 constructor Function: initializes the corresponding object. 7 8 9 to construct a code block format:Ten One { A Building Code Blocks - } - the Note: The brace of the construction code block must be located on the member position. - - - categories of code blocks: + 1. Construct the code block. - 2. Local code block. Curly braces are located within the method. Role: Shorten the life cycle of local variables and save a little bit of memory. + 3. Static code block static A */ at - classbaby{ - - intId//ID Card - -String name;//name in - //construct code blocks ... to { + //System.out.println ("The code that constructs the code block executes ..."); - the } * $ //with parameter constructorsPanax Notoginseng PublicBaby (intI, String N) { -ID =i; theName =N; + } A the //Non-parametric construction method + PublicBaby () { - } $ $ Public voidcry () { -System.out.println (name+ "Crying ..."); - } the } - Wuyi the - classDemo4 Wu { - Public Static voidMain (string[] args) About { $Baby B1 =NewBaby (110, "Dog Doll");//The dog's dog has iron eggs . -System.out.println ("Number:" + b1.id + "Name:" +b1.name); - /* - System.out.println ("number:" + b1.id + "name:" +b1.name); A + New Baby (112, "dog Left"); the new Baby (); - */ $ /* the New Baby (110, "Dog Doll"); the New Baby (112, "dog Left"); the new Baby (); the */ - } in}
1 /*2 things to keep in mind when building code blocks:3 1. When the Java compiler compiles a Java source file, the declaration statement of the member variable is advanced to the front of a class. 4 2. The initialization of member variables is actually performed in the constructor. 5 3. Once compiled by the Java compiler, the code block that constructs the code block is executed in the move constructor, executed before the constructor, and the code in the constructor is last executed. 6 4. The display initialization of the member variable and the code that constructs the code block are executed in the order of the current code. 7 */8 classDemo59 {Ten One A - //constructor Function - PublicDemo5 () {//constructor Function thei = 300000000; - } - - //Building Code Blocks//constructing the initialization of a code block + { -i = 200000000; + } A at inti = 100000000;//explicit initialization of member variables - - - - Public Static voidMain (string[] args) - { inDemo5 d =NewDemo5 (); -System.out.println ("i =" +d.i);//Shuang, Wu, Liu: to } +}
Java Road Construction code block considerations