[Javase Study Notes]-7.15 construct a code block

Source: Internet
Author: User

[Javase Study Notes]-7.15 construct a code block

In this section, let's look at a special code block, which is to construct the code block.

 

Here we will give an example to illustrate:

 

Class Person {private String name; {System. out. println ("the first code block of the Person class is executed");} Person () {System. out. println ("No parameter constructor executed"); this. name = "";} Person (String name) {System. out. println ("constructor with name parameter executed"); this. name = name;} public void speak () {System. out. println ("name:" + name) ;}} class ConBlockTest {public static void main (String [] args) {Person p1 = new Person (); p1.speak (); person p2 = new Person ("little Kobe"); p2.speak ();}}
In this example, we can see that there is a code block in the Person class, which is not modified by the static keyword. This is the constructed code block mentioned in this section. Why? Let's look at the running result:

 

We can see that when we create two objects, the code block is executed, and the constructor is called only when the corresponding object is created.

Therefore, the function of constructing a code block is:Initialize the same part of all objects.

Our constructor is to perform targeted and unique Initialization on the corresponding object.

 

Which of the following statements is executed first to construct the constructor of a code block? Let's look at the Code:

 

Class Person {private String name; {// The first construction code block System. out. println ("1st code blocks of the Person class are executed");} Person () {System. out. println ("No parameter constructor executed"); this. name = "";} Person (String name) {System. out. println ("constructor with name parameter executed"); this. name = name;} public void speak () {System. out. println ("name:" + name) ;}{// second construction code block System. out. println ("2nd code blocks of the Person class are executed ");}}
The result is as follows:

 

We can see that the Construction Code blocks in two different locations have been executed before the constructor is executed.The construction code block takes precedence over the construction function execution.

 

Therefore, when we need to initialize all objects with the same code block, we can use the constructed code block. For example, in the above example, people will cry at birth, then we can use the constructed code block to initially cry this function:

 

Class Person {private String name; {cry ();} Person () {this. name = "";} Person (String name) {this. name = name;} public void cry () {System. out. println ("wow");} public void speak () {System. out. println ("name:" + name );}}
In this way, we encapsulate all the object crying functions into a construction code block, and the innovative object will be executed first, which achieves the desired functions well.

 

 

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.