6.3 Scope of a Simple Name

The scope of a declaration is the region of the program within which the entity declared by the declaration can be referred to using a simple name:


	class Test {
		int i = j;				// compile-time error: incorrect forward reference
		int j = 1;
	}


	class Test {
		Test() { k = 2; }
		int j = 1;
		int i = j;
		int k;
	}

even though the constructor (§8.6) for Test refers to the field k that is declared three lines later.

These rules imply that declarations of class and interface types need not appear before uses of the types.

In the example:

package points;


class Point {
	int x, y;
	PointList list;
	Point next;
}

class PointList {
	Point first;
}

the use of PointList in class Point is correct, because the scope of the class type name PointList includes both class Point and class PointList, as well as any other type declarations in other compilation units of package points.