• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difference between static and final

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,

Can any one tell the difference between static and final (AND) static final and fial static.
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Final variables are instance variables, whose values cannot be changed after it is initialized ( either in the c'tor or while declaring ).
But Static variables belongs to a class. This 'll be shared amoung all instances. So when you change the value of a static variable in an instance it gets reflected in all the instances.
[ December 15, 2005: Message edited by: Srinivasa Raghavan ]
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Srinivas, do final variables necessarily have to be instance variables? How about Math.PI and Math.E?
As for differences between static and final, we're comparing apples and oranges. They're applied in different contexts and differentiating between seems (to me) just a listing of their respective properties. For examples, differences between protected and private seems to make "more sense".
final static and static final should be the same. The order of applying modifiers shouldn't change the nature of the end result (can anyone think of a counterexample?).
Sashi
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Below are my inputs on this.

final:

These variables(every object has their own copy), can be assigned to a value only one time. This can be either

1. during declaration itself(Well, if you want to initialise the final variable in declaration itself, I'll suggest to make it static. Because it doesn't make sense that all objects of that class has same value for a variable, each having their own copy)
2. Or some where inside the methods/contructors(in this case, it should not be static).

static:

These variables are called as class variables ie only one variable that is shared by all instances of that class. But the value can be modified at any time by any object(ofcourse of the same type)

static and final:

Combined effect of above two ie only one variable that is shared across all the instances and should be assigned value either during declaration or in a static block(not inside a static method/constrcutor/methods)

As said above, there is no difference between changing the sequence of final and static in a varaible declaration.


Hope this helps!
 
Sasikanth Malladi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lakshmanan, a final variable needs to be initialized no later than the end of a constructor call and not in any other method.
From the KB book:


The rule is: if you declare a final instance variable,you�re obligated to give it an explicit value, and you must do so by the time the constructor completes.


Also, a final variable needs to be explicitly initialized: it won't be given a default value by the compiler.
Sashi
 
Lakshmanan Arunachalam
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah... Good catch !!!
Got it, because a method can be called 'n' number of times, hence assigning a value to a final instance variable inside a method will throw compile err.

Thanks Sashi
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic