Forums Register Login

Final Variable Declaration

+Pie Number of slices to send: Send
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);

}
}
+Pie Number of slices to send: Send
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
Don't destroy the earth! That's where I keep all my stuff! Including this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 803 times.
Similar Threads
Global final v/s Local final
final variable
Sharpen your pencil, chapter numbers and statics
what is the difference between final and compile constant?
Final Variable
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 09:45:12.