Static variables of Java, member variables, static blocks of code, loading order of building blocks

Source: Internet
Author: User

Before starting this blog writing, please look at a classic Java face question

public class statictest{

    public static void Main (String args[]) {
Staticfunction ();
}
static Statictest st = new Statictest ();
static{
System.out.println ("1");
}
Statictest () {
System.out.println ("3");
System.out.println ("a=" +a+ "b=" +b);
}
public static void Staticfunction () {
System.out.println ("4");
}
{
System.out.println ("2");
}
int a=100;
static int b=112;
}
The final answer is:
2
3
a=100 b=0
1

Is there some surprise in this execution? So now this rookie is here. Analysis: for static member variables and static code blocks, the order of initialization is in the order of occurrence, when there is a static code block of the parent class or a static member variable of the parent class, regardless of the order of occurrence, the parent class is initialized first,

A detailed interview question, which is analyzed from the life cycle of the class and the initialization of the object:

1. The life cycle of the class is: load-and-use-------------------------------------
2. The preparation phase of the class needs to be done to allocate memory for class variables and set default values, so class variable st is null, B is 0; (Note that if the class variable is final in the loading phase, the initialization is completed, you can set the B to final);
3. The initialization stage of the class needs to do is to execute the class constructor (the class constructor is the compiler collects all static statement blocks and assignment statements of class variables in the order of the statements in the source code to merge the generated class constructor, the object is constructed by init, the class is constructed by Cinit, can be seen in the stack information), So first the assignment statement of the first static variable is St = new Statictest (), the object is initialized, the object is initialized to initialize the member variable and then the construction method is executed, so the print 2-> setting a is 110-> to execute the construction method (print 3, At this point A has been assigned a value of 110, but B just set the default value of 0, did not complete the assignment action, and so on after the initialization of the object to continue to execute the previous class constructor statements, the next detailed:

Execute the statement first: static Statictest st = new Statictest (); This statement is equivalent to static{statictest st = new Statictest ()},statictest st = New Statictest (), which first executes the member variables of the Statictest class and constructs a block of code, so it first executes the{System.out.println ("2");this constructs a block of code, results in 2, and then executesint a=100; this statement. Then execute the Statictest () {} construction method and print out the3,a=100 b=0, finally executedstatic{System.out.println ("1"); This static code block, the result is 1

Static variables of Java, member variables, static blocks of code, loading order of building blocks

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.