Java Note 1: class member initialization sequence

Source: Internet
Author: User

I have been reading thinking in Java recently. I feel that thinking is very detailed and many things have never been touched. It is a good book worthy of careful taste by Java programmers.

Today, I read the section about class member initialization. In the past, the initialization sequence of the members was not considered so much and I did not care about the initialization sequence. I have studied it carefully today.

In a class, the first Initialization is a static member, that is, a member with the keyword static modifier. When a class is used, that is, when an object is created or a static method of the class is called, the static members are initialized, and the static data occupies only one storage area in the memory, no matter how many objects are created, static data is shared by all objects.

Initialize static members before initializing non-static members. There is also an initialization order between non-static members. The variable definition order determines the initialization Order (no matter where the member variables are defined, they will be initialized before the function call)

The constructor Initialization is executed only after the initialization of the member is directly defined.

For example:

1 public class house {2 // for non-static member variables, whoever defines who is first initialized first. The sequence is W1, W2, and W3 window W1 = new window (1); 4 Public House () {5 system. out. println ("House ()"); 6 W3 = new window (33); 7} 8 window W2 = new window (2); 9 void F () {10 system. out. println ("F ()"); 11} 12 static void F1 () {13 system. out. println ("static F1 ()"); 14} 15 window W3 = new window (3); // After function F, however, 16 static window W4 = new window (4) is obtained before the f call ); // The static member variable is first initialized 17/** 18 * @ Param args19 */20 public static void main (string [] ARGs) {21 House H = new house (); 22 h. F (); 23 house. f1 (); 24} 25 26} 27 class window {28 public window (INT maker) {29 system. out. println ("window (" + maker + ")"); 30} 31}

The execution result is as follows:

1 Window(4)2 Window(1)3 Window(2)4 Window(3)5 House()6 Window(33)7 f()8 static f1()

This clearly shows the initialization sequence of the members.

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.