[Javase Learning note]-7.15 Building blocks of code

Source: Internet
Author: User

In this section, we look at a special block of code that constructs a block of code.


Here's a simple example to illustrate:

Class Person{private String name;{ System.out.println ("The first block of code of the person class is executed");} Person () {System.out.println ("parameterless constructor executed"); this.name = "Little Baby";} Person (String name) {System.out.println ("the constructor with the name argument is executed"); this.name = name;} public void Speak () {System.out.println ("name:" +name);}} Class Conblocktest{public static void Main (string[] args) {person P1 = new person ();p 1.speak (); person P2 = new Person ("Little Kobe");p 2.speak ();}}
We see in this example that there is a block of code in the person class that is not decorated with the static keyword, which is what we call the construction code block in this section, and why we look at the results of the operation:


We obviously saw that when we created two objects, the code block was executed, and the constructor was called only when the corresponding object was created.

So the code block is constructed to initialize all objects in the same part.

Our construction method is to target the corresponding object of the unique initialization.


So which of the constructors that constructs the code block executes first? Let's look at the code:

Class Person{private String name;{ The first constructs a block of code System.out.println ("the 1th block of code for the person class is executed");} Person () {System.out.println ("parameterless constructor executed"); this.name = "Little Baby";} Person (String name) {System.out.println ("the constructor with the name argument is executed"); this.name = name;} public void Speak () {System.out.println ("name:" +name);} {//Second Construction code block SYSTEM.OUT.PRINTLN ("2nd code block of the person class is executed");}}
We look at the results:

We see that the construction code blocks of two different locations have been executed before the constructor is executed, so the Construction code block takes precedence over the constructor execution.


So, when we need to have all the objects have the same initialization, we can use the construction code block to implement, such as the above example, the person will cry at birth, then we can use the construction code block to start crying this function:

Class Person{private String name;{ Cry ();} Person () {this.name = "little Baby";} Person (String name) {this.name = name;} public void Cry () {System.out.println ("Wah");} public void Speak () {System.out.println ("name:" +name);}}
So we wrap all the object crying function into a block of construction code, the Innovation object will be the first execution, very good implementation of the function we want.


[Javase Learning note]-7.15 Building blocks of code

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.