In the following code class outer{ static int a; static final int b; static final int c=1; static{ b = 2; }
class inner{ static final int x = a; static final int y = b; static final int z = c; } } I get 2 errors global.java:10: inner classes cannot have static declarations static final int x = a; ^ global.java:11: inner classes cannot have static declarations static final int y = b; ^ Why is it an error to initiallise a static final field in an inner class to a static field or final field in the enclosing class when both are actually accessible from the inner class. Moreover static final int z = c; is ok as I have initiallised the static final field c in its declaration.But static final int y = b; gives an error when I have initiallised the static final field b in the static block of the enclosing class. Please explain.Could you give me the logical reasoning as to why was this done.I mean the thought behind this design.
nothing to do with initialization. cant have static variables in inner classes.
mridul das
Greenhorn
Joined: Jul 15, 2005
Posts: 19
posted
0
we cant have static variables in an inner class but static final variables are allowed.As you can see static final int z = c; compiles.But not static final int x = a; static final int y = b; The problem is with innitiallisation.The way we are initiallising the final static variables x and y.
1. You cannot use a static int final variable without initialization (This RULE holds true even if you are not using inner class concept etc).
But why the non final static variable which has been initialized in the outer claess - giving error on being used in the inner class - but does not give error on being used by the outer class for the initialization of final static variables ? This doubt remains...
The rules aimed at using proper English are well intentioned but unecessary and in fact a burden for those who aren't particularly adept typists. As for the name rule, it's also well intentioned but rather myopic and provincial: is Jan Ilsa Gumbhat Norwegian for Java Blows Chunks? Also, if they don't particularly like you and find your name the least bit whimsical, even a real one like mine, they may use it as a child-like weapon.
We have a specific forum -- the JavaRanch forum -- for discussing this site and its rules. If you have a complaint, or would like to continue this discussion, please do so there. [ August 07, 2005: Message edited by: Ernest Friedman-Hill ]