L
lurer
class Out
{
int x;
{
k = 5; //valid
x = k; //invalid
}
public int k;
}
{
int x;
{
k = 5; //valid
x = k; //invalid
}
public int k;
}
Out.java:6: illegal forward reference
x = k; //invalid
^
1 error
so "forward assignment" is ok?
k = 5; //"forward assignment"
x = k; //compiler error because of forward reference
?
Jussi said:Seems so. A simplest web search suggests that the JLS, possibly in
section 8.3.2, requires declaration before using a variable in an
initializer "not on the left hand side of an assignment".
Jussi said:Seems so. A simplest web search suggests that the JLS, possibly in
section 8.3.2, requires declaration before using a variable in an
initializer "not on the left hand side of an assignment".
Use of class variables whose declarations appear textually after the use is sometimes restricted, even though these class variables are in scope. See §8.3.2.3 for the precise rules governing forward reference to class variables.
The declaration of a member needs to appear textually before it is used only if the member is an instance (respectively static) field of a class or interface C and all of the following conditions hold:
* The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer of C.
* The usage is not on the left hand side of an assignment.
* The usage is via a simple name.
* C is the innermost class or interface enclosing the usage.
A compile-time error occurs if any of the four requirements above are not met.
Use of instance variables whose declarations appear textually after the use is sometimes restricted, even though these instance variables are in scope. See §8.3.2.3 for the precise rules governing forward reference to instance variables.
Joshua said:I suppose that makes sense.
k = 5; is correct because the assignment of k works, but if x=k; were
allowed, this could happen:
int x;
{
x = k;
}
int k = x;
What is x and k? k is defined initially in terms of x, which is
initialized to k, so no definite assignment can be given them.
In short, forward assignment is harmless, so it is allowed; forward
reference opens up a can of worms, so it is forbidden.
Use of class variables whose declarations appear textually after the use is sometimes restricted, even though these class variables are in scope. See §8.3.2.3 for the precise rules governing forward reference to class variables.
and
The declaration of a member needs to appear textually before it is used only if the member is an instance (respectively static) field of a class or interface C and all of the following conditions hold:
* The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer of C.
* The usage is not on the left hand side of an assignment.
* The usage is via a simple name.
* C is the innermost class or interface enclosing the usage.
A compile-time error occurs if any of the four requirements above are not met.
Use of instance variables whose declarations appear textually after the use is sometimes restricted, even though these instance variables are in scope. See §8.3.2.3 for the precise rules governing forward reference to instance variables.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.