sona<br />SCJP
Originally posted by Manfred Leonhardt:
Hi Kapil,
Order is important in how you declare variables. In your example you are trying to use variable k before the compiler knows what it is. The final keyword doesn't change the loading order. Static variables are loaded when the class is loaded in the order in which they are declared. Instance class variables are loaded when an instance is created in the order in which they are declared.
Regards,
Manfred.
Originally posted by sona nagee:
class a{
final int i=1;
int j=i*k; // k is not defined yet so how can u use it
final int k=3;
public static void main(String h[])
{
a ob=new a();
System.out.print(ob.j);
}
}
rptl
15.11.1 Field Access Using a Primary
The type of the Primary must be a reference type T, or a compile-time error occurs. The meaning of the field access expression is
determined as follows:
If the identifier names several accessible member fields of type T, then the field access is ambiguous and a compile-time error occurs.
If the identifier does not name an accessible member field of type T, then the field access is undefined and a compile-time error occurs.
Otherwise, the identifier names a single accessible member field of type T and the type of the field access expression is the declared type of
the field. At run time, the result of the field access expression is computed as follows:
If the field is static:
If the field is final, then the result is the value of the specified class variable in the class or interface that is the type of the Primary expression.
If the field is not final, then the result is a variable, namely, the specified class variable in the class that is the type of the Primary expression.
If the field is not static:
If the value of the Primary is null, then a NullPointerException is thrown.
If the field is final, then the result is the value of the specified instance variable in the object referenced by the value of the Primary.
If the field is not final, then the result is a variable, namely, the specified instance variable in the object referenced by the value of the Primary.
If the value of the Primary is null, then a NullPointerException is thrown.
If the field is final, then the result is the value of the specified instance variable in the object referenced by the value of the Primary.
The fastest and most reliable components of any system are those that are not there. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|