Java: Initialization blocks, static initialization blocks, execution order of construction methods

Source: Internet
Author: User

1. Static initialization block

  Static initialization blocks are only executed once when the class is first loaded , while static initialization blocks can only assign values to static variables and cannot be assigned to normal member variables.

2. (non-static) initialization block

 (non-static) initialization blocks are executed once each time the instance object is generated , and any variable can be assigned a value.

3. Construction method

  The construction method executes once each time the instance object is built

4. Execution order: Static initialization block---initialization block----construction method

The test code is as follows:

1  Public classSonextendsFather {2     Static intSnum = 0;3     intnum = 0;4      Public Static voidMain (string[] args) {5SYSTEM.OUT.PRINTLN ("-----");6         NewFather ();7SYSTEM.OUT.PRINTLN ("-----");8         NewSon ();9SYSTEM.OUT.PRINTLN ("-----");TenSystem.out.println ("Snum =" +snum); One     } A      -      PublicSon () { -System.out.println ("Son construction Method"); the     } -      -     Static { -         //num = 1; //Error: Cannot assign a value to a non-static variable +Snum = 1; -System.out.println ("Son static initializer"); +     } A      at     { -System.out.println ("Son initialization block"); -     } -      - } -  in classFather { -     { toSystem.out.println ("Father initialization Block"); +     } -      the      PublicFather () { *System.out.println ("Father construction Method"); $     }Panax Notoginseng      -     Static { theSystem.out.println ("Father static initializer"); +     } A}
View Code

The test results are as follows:

Summary:
  1. The static initialization block has the highest precedence, that is, the first execution, and only when the class is loaded for the first time ;
  2. Non-static initialization blocks and constructors are executed and executed once each time the object is generated ;
  3. The code for a non-static initial block is executed before the class constructor . Therefore, in order to use, we should develop the habit of writing the initialization block before the constructor, which is easy to debug;
  4. Static initialization blocks can be used either to initialize static member variables or to execute initialization code ;
  5. Non-static initialization blocks can be code reused for multiple overloaded constructors.

Executes only once, when the class is first loaded.

Java: Initialization blocks, static initialization blocks, execution order of construction methods

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.