This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
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
Author

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
Yes.
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: http://aspose.com/file-tools
 
subject: about final variable