[Javase Learning note]-8.8 Building blocks of code

Source: Internet
Author: User

In this section, we'll look at a special block of code. That is to construct the code block.


Here's a simple example to illustrate:

Class Person{private String name;{ System.out.println ("The first code block of the person class is run");} Person () {System.out.println ("No parameter constructor is run"); this.name = "Baby";} Person (String name) {System.out.println ("the name parameter constructor is run"); 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, which is not modified by Statickeyword, 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 the code block was run when we created two objects. The constructor is called only when the corresponding object is created.

So the code block is constructed to perform the same part of the initialization of all objects.

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


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

Class Person{private String name;{ The first constructs a code block SYSTEM.OUT.PRINTLN ("The 1th code block of the person class is run");} Person () {System.out.println ("No parameter constructor is run"); this.name = "Baby";} Person (String name) {System.out.println ("the name parameter constructor is run"); 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 run");}}
We look at the results:

We see that the construction code blocks of two different locations are already running before the constructor is run. Therefore, constructing a code block takes precedence over the constructor function.


So, when we need to have the same initialization for all of our objects, we can use the construction code block. In the example above, when a person is born to cry, 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);}}
This allows us to encapsulate the function of all objects crying into a block of construction code. In the Innovation object is the first run, very good implementation of the function we want.


[Javase Learning note]-8.8 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.