1. 程式人生 > >javac之向前引用

javac之向前引用

highlight println pub .com oracle error sta ann erro

可以參考JLS7:https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.2.3

public class Test5 {
	
	int a = m1();

	public int m1() {
		System.out.println(i);  // 0
		return i;
	}

	int b = (new Object() {
		public int t() {
			System.out.println(i); // 0
			return i;
		}
	}).t();
	
	{
		i = 100;
		System.out.println(this.i); // 100 
		//i = i+1; // error Cannot reference a field before it is defined
		//System.out.println(i); // error Cannot reference a field before it is defined
	}
	
	//int k = i+1; // error  Cannot reference a field before it is defined

	int i = 2;

	public static void main(String[] args) {
		Test5 t = new Test5();
		System.out.println(t.i); // 2
	}

}

javac之向前引用