This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

when would using final variables be complex?

 
H Melua
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I've been looking at declaring variables as final, and there seems to be a recommendation for making all variables final unless the variable needs to be initialized more than once.

I haven't come across any disadvantages for using final variables... and I believe there is always a disadvantage in anything...

So, when would you not recommend using final variables? and when do you think it will only add complexity, and should not be used?

Thanks
HannaH
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Making the variable final is preferred, if like you had already posted, the value can only be initialized once.

The other caveat is that any field labeled final must be initialized while the class is being constructed. If you can't assign a value until later (like through a setter method) then you can't use the final keyword. Ditto for static finals and static initializers (i.e. same thing goes for...).


-- edit --

Unintended mis-pelled word with alternate meaning ;(
[ August 12, 2008: Message edited by: Steve Luke ]
 
H Melua
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Luke:
Making the variable final is preferred, if like you had already posted, the value can only be initialized once.

The other caveat is that any field labeled final must be initialized while the class is being constructed. If you can't assign a value until later (like through a setter method) then you can't use the final keyword. Diddo for static finals and static initializers.


Thanks, thats a good point...

I had to google "Diddo" to know what it meant lol
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by H Melua:


Thanks, thats a good point...

I had to google "Diddo" to know what it meant lol


Doh... Sorry for that. I meant Ditto!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic