File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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.
I agree. Here's the link:
subject: Final Variable Declaration
It's not a secret anymore!