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:
It looks like compiler will not consider un initialized usages in methods. Any further comments on this ?
The compiler will only fail on uninitialized local variables (in methods). All class variables are initialized automatically.
... 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.
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.