| Author |
Illegal use of a qualified name in static initialization block
|
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
The following does not compile with the compiler saying it cannot assign to a final variable: However if I replace the commented line by plain old it works. Any ideas why the fully qualified name of the class variable is not allowed? [ June 19, 2006: Message edited by: Barry Gaunt ]
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
Mr. C Lamont Gilbert
Ranch Hand
Joined: Oct 05, 2001
Posts: 1170
|
|
Originally posted by Barry Gaunt: ...Any ideas why the fully qualified name of the class variable is not allowed?
FinalStaticInit != StaticFinalInit
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
Originally posted by Mr. C Lamont Gilbert: FinalStaticInit != StaticFinalInit
Sorry, that was a typo - I will correct my code above. (I would have had an undefined symbol StaticFinalInit in that case) But the question is still the same as before. The error is: "the final field FinalStaticInit.i cannot be assigned". Thanks [ June 19, 2006: Message edited by: Barry Gaunt ]
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
My guess is that the compiler is first looking at the modifier of the variable referred to, and since, in FinalStaticInit, the variable named i is marked final, the compiler stops and throws a syntax error. Consider these classes. When we try to compile Test.java, we get the error even though both Test and Test6 define static final variables and don't assign them a value.
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24041
|
|
This is a job for Jim, methinks. I can tell you that Jikes says
*** Semantic Error: The final field "i" may not be assigned in a qualified expression. Use a simple name instead.
so this sounds like it may just be a straightforward rule someplace in the JLS.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
It could be a straightforward rule somewhere, but it's not jumping out at me. On the other hand I can't think of any reason why you should ever need to use a qualified name when assigning a static final. This can only ever be done from within the class that declares the final, and so the field is already accessible without qualification. If for some reason you were to also declare a local variable with the same name... well then I guess it would become impossible to initialize the field (while the local is in scope). That's pretty easy to avoid though. So I'm not sure why this rule exists (or where it's defined), but it doesn't seem to cause any major problems. [ June 19, 2006: Message edited by: Jim Yingst ]
|
"I'm not back." - Bill Harding, Twister
|
 |
Ken Blair
Ranch Hand
Joined: Jul 15, 2003
Posts: 1078
|
|
|
Just so we're clear you can assign to a static variable using a qualified name. What makes this special is the final modifier. I'm not sure where the rule is but I suspect it has to do with class initialization because there's a lot of special rules surrounding that.
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
There's some text at the beginning of JLS3 8.3.2.3 that looks like it might have something to do with this. Unfortunately it seems to be one of the more poorly-written passages of the JLS which I have seen; I really can't tell what they're trying to say. Maybe I'm missing something. There are four bullet points which are either preconditions for invoking a rule (in which case if they're false, the rule simply has no effect) or they're rules which case compilation errors if they're broken. Good luck determining which though, as there's evidence for both views. But one condition which may or may not cause a compilation error if violated is that "the usage is via a simple name". Sounds relevant to our problem - too bad it doesn't seem to answer it. [ June 19, 2006: Message edited by: Jim Yingst ]
|
 |
Ken Blair
Ranch Hand
Joined: Jul 15, 2003
Posts: 1078
|
|
|
That's defining when you cannot have forward references. In other words, you can't have a forward reference if it's used in a static variable initializer or static initializer in the same class or interface it's declared in and where the usage isn't it being assigned. This makes sense given the initialization of a class and the fact that it occurs in the same order they appear textually. Without those rules you would be able to refer to a static variable that hadn't actually been initialized.
|
 |
Ken Blair
Ranch Hand
Joined: Jul 15, 2003
Posts: 1078
|
|
Jim, check out the rules governing definite assignment (chapter 16), specifically I find this paragraph interesting:
Similarly, every blank final variable must be assigned at most once; it must be definitely unassigned when an assignment to it occurs. Such an assignment is defined to occur if and only if either the simple name of the variable, or its simple name qualified by this, occurs on the left hand side of an assignment operator. A Java compiler must carry out a specific conservative flow analysis to make sure that, for every assignment to a blank final variable, the variable is definitely unassigned before the assignment; otherwise a compile-time error must occur.
That would seem to indicate that using a name qualified by anything other than this isn't allowed.
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
Thankyou, all, for your responses. I wondered about this because I wanted to use the qualified variable name to emphasize the staticness of it. But as Jim pointed out its use is so close to the definition that it is hardly necessary. It's interesting to see the more informative message give by the Jikes compiler. Great responses! -Barry
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
[Ken]: That's defining when you cannot have forward references. I'm not sure it's successfully defining anything there, Ken. But I agree that's apparently what they were trying for. [Ken]: Jim, check out the rules governing definite assignment (chapter 16) Yes, that's definitely the place. I looked right past that front section and into the more specific rules. Good job.
|
 |
Rick Reumann
Ranch Hand
Joined: Apr 03, 2001
Posts: 281
|
|
This is one reason I really like what C# enforces. In C# you can't ever refer to a static var without reference to the class name itself. I sort of wish Java would adopt this. So for example if you have... class A() { static int i; } You would HAVE to refer to i as A.i and never just use "i" (At least that's my understanding back when I coded with it a while back).
|
 |
 |
|
|
subject: Illegal use of a qualified name in static initialization block
|
|
|