File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes final variable initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "final variable initialization" Watch "final variable initialization" New topic

final variable initialization

vijayk kumar

Joined: Jul 29, 2006
Posts: 18
Does a final variable initialized in overloaded constructors differently.
Then what about the necessity of intializing a variable at the declartion?
ramesh kumar

Joined: Aug 15, 2006
Posts: 25
we can only initialize final variables once.
Tanka Banerjee
Ranch Hand

Joined: Aug 16, 2006
Posts: 64
Do it. final can't be reintialized
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
I'm not sure what these guys are celebrating with their . But to answer the question in a useful way, there is no requirement to initialize a final variable at declaration. For an instance variable, the requirement is that it needs to be assigned before the constructor completes. If you want t assign a final variable in a constructor, you must not assign it at declaration, since a final variable may be assigned only once. vijayk, does that answer your question?

"I'm not back." - Bill Harding, Twister
Dana Bothner-By
Ranch Hand

Joined: Jul 28, 2006
Posts: 37
Hmmm... Your first sentense no verb.

Anyway, don't forget to assign a value in *every* constructor:
marc weber

Joined: Aug 31, 2004
Posts: 11343


Please see Henry's warning about your display name under this other post. Basically, we need a last name too. Thanks!

(So far, you've used in 100% of your posts. :roll: )

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Jim covered final fields. Final locals need never be assigned, but must be "definitely assigned" before they are used.

Tony Morris
Java Q&A (FAQ, Trivia)
I agree. Here's the link:
subject: final variable initialization
jQuery in Action, 3rd edition