Deep understanding of initialization of final variable _java

Source: Internet
Author: User
Initialization position of final variable
The first is its definition, that is to say, when the final variable is defined, the value is assigned directly to it.
The second is in the constructor. And before Java1.1, only the value can be given at the time of definition.
Three is at the beginning of the code block {} or static{}
Copy Code code as follows:

public class Initorder {
{
System.out.println ("Before---field");
System.out.println ("d1=" +d1);
D1 = 3;
System.out.println ("d1=" +d1);
}
static {
System.out.println ("Before---static field");
System.out.println ("d2=" +d2);
D2 = 3;
System.out.println ("d2=" +d2);
}
final int a1 = 1;
Final int B1;
final int C1;
final int d1;
Final int E1;
static final int a2 = 1;
static final int B2;
static final int C2;
static final int D2;
static final int E2;

{
System.out.println ("After---field");
System.out.println ("c1=" +c1);
C1 = 4;
System.out.println ("c1=" +c1);
e2 = 3;
}
static {
System.out.println ("After---static field");
System.out.println ("c2=" +c2);
C2 = 4;
System.out.println ("c2=" +c2);
e1 = 3;
}
Public Initorder () {
B1 = 2;
b2 = 2;
}
public static void Main (string[] args) {
Initorder order = new Initorder ();
System.out.println ("c1=" +order.c1);
System.out.println ("c2=" +order.c2);
System.out.println ("d1=" +order.d1);
System.out.println ("d2=" +order.d2);
}
}

Description: All of the above comments are grammatical errors
Output results:
Before---static field
After---static field
C2=4
Before---Field
After---Field
C1=4
C1=4
C2=4
D1=3
D2=3
Results Analysis:
1. The comparison a1,a2 basically does not have any question, the definition is initialized
2. Contrast B1,B2 initialization in the constructor, B1 there is no problem, B2 problem, because the call of the constructor is static variable after the B2 is static, so it will error
3. Compare C1,D1 found that initialization is not a problem, the problem is to use output statements. The D1 is an error in the initialization code block regardless of where the output statement is, because the initialization code block location of the D1 is in front of the variable D1 definition, and the initialization order of the variables in Java is seen in the initialization order of the variables in Java. The initialization order of the normal variable and initialization code block is based on the position, so the output uses the D1 variable, so there's a mistake, but I don't understand why it's not an error to initialize D1 here, and you can call it in the main function, do you want to take a look at the Java Virtual machine? There is no problem with C1 following the output statement. It is well understood that the preceding question is not initialized before the big one.
4. For C2,D2, it's the same as 3.
5. It is obviously not possible for E1 to be placed in a static code block because the static code block was first loaded, and E1 was not added
6. For E2 also, normal code blocks are loaded later than static variables, so it's not going to work.

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.