The instance field variable initializers of a class correspond to a sequence of assignments that are executed immediately upon entry to any one of the instance constructors (§10.11.1) of that class. The variable initializers are executed in the textual order in which they appear in the class declaration. The class instance creation and initialization process is described further in §10.11.
A variable initializer for an instance field cannot reference the instance being created. Thus, it is a compile-time error to reference this in a variable initializer, as it is a compile-time error for a variable initializer to reference any instance member through a simple-name. In the example
class A
{
int x = 1;
int y = x + 1; // Error, reference to instance member of this
}
the variable initializer for y results in a compile-time error because it references a member of the instance being created.
Do'stlaringiz bilan baham: |