• Post Reply Bookmark Topic Watch Topic
  • New Topic

static vs. final  RSS feed

 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not really sure I understand the functional difference between a static and final variable/field. Oracle defines Class Variable as:

Class Variables (Static Fields) A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances. The code static int numGears = 6; would create such a static field. Additionally, the keyword final could be added to indicate that the number of gears will never change.

If static will have the same value regardless of how many times it's used, then why use final (or vice versa)?
 
Dave Tolls
Ranch Foreman
Posts: 3067
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static - class variable, so only one for the class, unlike instance variables, where you have one per instance.

In the above there will only ever be one 'someStatic', but there will be several 'someAttribute', one per instance of SomeClass.

If 'final' were added to either, then you would not be able to change the value once set.
'static' does not mean 'final'.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question static vs final comes up often enough, and I wonder why. Here, however, the reason is pretty obvious. Please tell us where that comes from: is it the Bicycle example near the beginning of the Java Tutorials? That bit you have quoted is by no means clear, and there are other bits of the Java Tutorials which people find difficult to read.

Things final are designed not to change.
private final String name = "Ryan";
That is an instance field: one copy of the name per object, and that points to "Ryan". And it will always point to "Ryan". And that String will always be "Ryan" because (unlike a lot of classes) Strings are immutable. It says so here.
There are restrictions about final fields. They must be initialised once and once only and never then reassigned, so they must have a definite value by the time the constructor completes.
private final Bicycle bike
// ...
bike = new Bicycle(...);

That means my Bicycle will always be a particular Bicycle. I am not allowed to buy a new one, nor sell it, nor is anybody allowed to steal it (not that that seems to stop them round here ‍). This Bicycle, however, is a mutable object. At one point it will be a Bicycle travelling at 0mph, then 12 mph, then 45mph (I kid you not! Try going down Clay Bank or Boulby Bank.) But it ain't static, so that means everybody will have a different Bicycle object. Nobody can change it, but all the objects are different.

Static to follow.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are completely different concepts.

You can have a final field means that it can't be changed once set. Let's say you've got an object that has an ID number that is set when it is created, and never changed. Then you'd declare the variable as final. But you wouldn't make it static, because that would mean that every object has the same ID number.

A static variable belongs to the class, not individual objects, but it's still allowed to change. Imagine having a variable in a class to count the number of instances that have been created. You'd want to use a static variable that you increment every time an instance is created. So you can't make it final, because it keeps changing.

A variable that is both static and final is a constant - that's the standard way in Java to declare constant values.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you have been told, static is one per class. you should use final as much as possible and static as little as possible, because you usually want things to belong to objects. Things static belong to the class, not to an object.

A static field:-
  • 1: One copy per class, whether you have 1 object or 10000000 objects.
  • 2: One copy per class, even if you have no objects of that class.
  • 3: It is shared between objects: all objects have access to that static value.
  • 4: It doesn't work the other way round. Things static don't have access to instance members of the class.
  • 5: You cannot call instance members by ClassName.member, but you should always call static members (if not private) by ClassName.member.
  •  
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12565
    49
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you had a dog class, you could define the latinName as a static variable. It doesn't matter if you have one Dog, one thousand Dogs, or zero Dogs, the latinName will always be "Canis lupus familiaris".

    However, the for commonName, you would need one per actual Dog. It would contain "Spot" or "Fido" or "Rufus". If you made commonName final, then once you give a dog a name, it cannot ever be changed.
     
    Campbell Ritchie
    Marshal
    Posts: 56576
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    … and if you make latinName final, the species will be called Canis lupus familiaris for ever and ever and ever.

    Which would annoy the taxonomical chappies who love changing Latin names. They appear to do it more with plants than animals, however.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!