This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
final is a constant whose value can not be changed. So, if a final variable is declared at class level and assigned value there, does it become static as well ?
Do final variables have to be declared at class level only OR can they be declared at local level also ? Does a final variable has to be initialized when its declared or can it be initialized anywhere ?
final and static are completely different things. final means you can't change the value, means declaration and initialization comes in the same place and you can't reinitialize it. it can always be a method local variable as well.
static on the other hand is a class level variable. Declaring a class level variable as final does not make it implicitly static. you have to explicitly mention it as static.
nirjari patel wrote:final is a constant whose value can not be changed.
Actually, final has lots of uses; but for a variable, it's a value which can be set precisely once, and after that cannot be changed.
For an instance variable, it can either be set eagerly (ie, with '=') or in a constructor. For a static variable, it is normally initialized eagerly, but you may also be able to set it in a static initializer (don't know because I've never tried it; but I would expect so).
Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here