Forward references in Java __java

Source: Internet
Author: User

In the study of the deep understanding of Java Virtual Machines, the chapter on initialization of a class mentions that a static statement block can only access a variable defined in a static statement block, a variable that is defined after it, that can be assigned in the previous static statement block, but cannot be accessed.

And give an example:

The public class MyClass {
	static {
		i = 0;//Assign a value to a variable to be normal through
		System.out.println (i);//compiler prompts illegal forward reference
	}
	
	static int i = 1;
	
}
After that, there were two questions.

1. We know that the initialization order of class variables and static statement blocks is in the order in which they occur, and when i = 0 is executed, the static int i = 1 is not executed and the undefined error of the variable is not reported.

2. Since i = 0 there is no problem, why not be accessible.


With these questions to see some of the internet on the forward-referenced blog, after some of their own understanding.

See three examples:

First Example:

public class MyClass {
	    void method ()  
	    {  
	        System.out.println (myvar);  
	    }  
	    String MyVar = "var value";  
	    
	    public static void Main (string[] args) {
			new MyClass (). method ();
Output:var value
In this example, you can see that the generated object calls the method () methods, which can be accessed to the MyVar variable, because when the object is new, the storage space is first allocated and initialized for the member variable. When the methods () method is executed, there is already a reference named MyVar in the MyClass object in the heap and initialization points to the string "var char". So you can access the variable and print it out.


A second example:

public class MyClass {
int method () {return n;}  
int m = method ();
int n = 1;
}
Output:
//1
//0
For this example, print N and M, then output 1 and 0 in turn. Now analyze the process of object creation to explain such a result. After the new MyClass () is executed, first a piece of the heap is marked with a zone store object, and the default initial value is assigned to the member variable (similar to the preparation phase of the class loading process to assign a default value to a static variable not final variable), It is then initialized in the order in which the member variables appear (this time you can run Java code), and then the constructor completes the creation of the object after all the member variables have been initialized.

Now, based on the analysis above, M and N start with 0, then initialize, execute M=method (); This time n is not initialized or the default value of 0, so the return value is assigned to M,m is 0. And then initialize N, and after the int n = 1, n changes from 0 to 1. So m=0,n=1 after the object creation is complete. The output is not difficult to explain.


A third example:

public class MyClass {
 
    int m = n;   Illegal forward reference
    {
    	n = 0;
    	SYSTEM.OUT.PRINTLN (n); Illegal forward reference
    }
    int n = 1; 
		    
}

When compiling this code, the compilation period fails, prompting for illegal forward references. It seems that in this example, int m=n and the last example int m = method (); The effect of method () {return n;}n is the same. In fact, if the compilation can pass, the effect is the same. According to our analysis, int m = N and code block println (n) is not a problem, you can give m has not initialized n the default value of 0 and print n default value of 0, but Java does not allow us to do so. Think about it and you'll understand that this is Java for security reasons: If the field is initialized, be sure to prevent access to the default value (which can be assigned but not accessible) in the program before initialization, which can cause a lot of unexpected problems.

In the article http://blog.csdn.net/xdugucc/article/details/78220518 of the constructor polymorphic behavior, we have seen the problem caused by this practice. So the compile period checks for and avoids the phenomenon that the variable is not fully initialized but is referenced in the previous case, but if the int m = Method () is like the second example; Method () {return n;}, the compiler is powerless, because at compile time it cannot know what value is assigned to M. So the compiler does not complain, but we want to avoid this phenomenon.


Now, the first example of this article is clear, although the three examples are based on the object creation process, and the first example is based on the class loading process, but the meaning is really interlinked. For variables that have an initial value, avoid accessing the default value of the variable before initialization. But the assignment is OK, because the final variable is still initialized.



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.