permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why is implicit initialization of class final fields (static and nonstatic) not allowed? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why is implicit initialization of class final fields (static and nonstatic) not allowed?" Watch "why is implicit initialization of class final fields (static and nonstatic) not allowed?" New topic
Author

why is implicit initialization of class final fields (static and nonstatic) not allowed?

Alexander Shrago
Greenhorn

Joined: Aug 18, 2008
Posts: 14
My jre 1.6 compiler generate an error in case of final field (static and nonstatic) may not be initializated. It doesn't even depends on if this field is used.
It is strange because class fields (static and nonstatic but not final) may be implict initialized during declaration.
And it's strange because the compiler requires the initialization of final local variables only in case of their usage.

I want to understand the logic of it to prepare to SCJP exam ...
Achilleas Achix
Ranch Hand

Joined: Apr 18, 2011
Posts: 123



mine complains only about final static.


OCPJP 6.0
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Achilleas Achix wrote:mine complains only about final static.

And if you remove that error then it will complain about the instance final variable. Alexander, what is the use of a constant if you don't initialize it? Even if you want your final variable to have a value of 0, your code must have that assignment otherwise the whats the purpose of that final variable. As for the compiler error, once a final variable of a class (static or non-static) is not initialized, there is no way for the compiler to be able to check where it is used as there are many possible ways in which you can access the value of the variable. This is why the compiler forces you to initialize it.


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why is implicit initialization of class final fields (static and nonstatic) not allowed?
 
Similar Threads
fix the bug
Hurting My Brains
enum "initialization circularity"?
final instance variables
Two Questions: Why = null, and why actual variable not method?...