wood burning stoves 2.0*
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
Author

Final Variable Declaration

ZEESHAN AZIZ
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[]){
System.out.println(x);

}
}
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.
/Mike
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Final Variable Declaration
 
Similar Threads
Sharpen your pencil, chapter numbers and statics
what is the difference between final and compile constant?
final variable
Global final v/s Local final
Final Variable