aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Final Variable Declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Final Variable Declaration" Watch "Final Variable Declaration" New topic

Final Variable Declaration

Ranch Hand

Joined: Jan 15, 2001
Posts: 162
I am little confused about initializing final variables.
KM says
"final variable need not be initialized at its declaration, but it must be initialized before it is used"
In code below, I am not using final variable at 2, still I am getting compiler error that variable is uninitialized, why is it so
public class q{
static int x;
final static int y;//2
public static void main(String args[]){

Mikael Jonasson
Ranch Hand

Joined: May 16, 2001
Posts: 158
Final variables doesn't have to be initialized on declaration, but they must be initialized no later then at the constructor for the class they are in.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Final Variable Declaration