• 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

about 'balnk' final fields

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In the above code, I am using final static variable with out initialization. compiler supposed to give errors for such un initialized use of final fields.

But the above code works and o/p is:
printCount: 0
Main: 3

It looks like compiler will not consider un initialized usages in methods. Any further comments on this ?
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static class variables are initialized before static code blocks. Because you are using a primitive variable, it initializes to it's default value (0). You are allowed to assign the value only once, but if you do not assign a value on creation, Java will assign the default value - 0 for ints, null for objects, etc.

The compiler will only fail on uninitialized local variables (in methods). All class variables are initialized automatically.
 
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
Not everybody knows about this, but it is widely understood. Neil Gafter and Josh Block riff on this in Puzzle #49 in "Java Puzzlers." Quoting from that book,


... it is possible to observe a final static field before it is initialized, when it still contains the default value for its type... Final fields are constants only if the initializing expression is a constant expression... in summary, be careful of class initialization cycles.



Note that your variable here does not have a constant expression initializer -- it has no initializer, as appropriate for a blank final.
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The spec says:

It is a compile-time error if a blank final (�4.12.4) class variable is not definitely assigned (�16.8) by a static initializer (�8.7) of the class in which it is declared.



Your variable is assigned by a static initializer, you just access it before that happens.
 
reply
    Bookmark Topic Watch Topic
  • New Topic