aspose file tools*
The moose likes Beginning Java and the fly likes when would using final variables be complex? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "when would using final variables be complex?" Watch "when would using final variables be complex?" New topic
Author

when would using final variables be complex?

H Melua
Ranch Hand

Joined: Jan 04, 2005
Posts: 172
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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 ]

Steve
H Melua
Ranch Hand

Joined: Jan 04, 2005
Posts: 172
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: when would using final variables be complex?