• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Illegal use of a qualified name in static initialization block

 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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.
 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
reply
    Bookmark Topic Watch Topic
  • New Topic