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 about final variable 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 "about final variable" Watch "about final variable" New topic

about final variable

wei liu
Ranch Hand

Joined: Dec 06, 2001
Posts: 56
Can the final variable be initialized in the constructor???
Jim Hall
Ranch Hand

Joined: Nov 29, 2001
Posts: 162
A final variable can be initialized when it is declared, in an initializer block, or in a constructor.
Arathi Rajashekar
Ranch Hand

Joined: Nov 20, 2001
Posts: 112
Final Variables:-
There are 2 types of final variables.
1) instance final vars.
2) class level (static) final vars.
According to JLS:-Instance Final Variable
"The final var has to have an explicit value by the programmer, but it can be given either in the declaration statement itself like " final int j=20;" OR in one of the FOLLOWING instance floating block (also called as instance initializers) OR in ALL CONSTRUCTORS of the given class".
In Second Case: Static final Variable
Static final vars also HAVE TO HAVE a value explicitly assigned by the programmer and this init has to happen either in the declaration statement itself like 'static final int var1=100;' OR in one of FOLLOWING static floating blocks (also called as static initializers). Also note that in both cases instance final var and static final vars, once we assigned a value means we CAN'T CHANGE them again.
Note we can't initialize static final variable in constructor

[This message has been edited by Arathi Rajashekar (edited December 21, 2001).]

Arathi<br />Sun Certified Java Programmer
I agree. Here's the link:
subject: about final variable
It's not a secret anymore!