25

I have read this sentence in a book but I didn't understand it:

A field that is both static and final has only one piece of storage that cannot be changed.

Can anyone explain it for me?

Jonas
  • 121,568
  • 97
  • 310
  • 388
Ruba
  • 885
  • 4
  • 18
  • 29
  • Note that `final` also has concurrency implementations. A `final` field is guaranteed to be thread-safe in a way that other fields are not. See: my answer here: http://stackoverflow.com/questions/3974350/safe-publication-through-final/3974372#3974372 – andersoj Oct 25 '10 at 22:45

4 Answers4

52

The source of your confusion may be that the word "static" in english and it's meaning in Java are only loosely related.

A variable defined in a class Cat in the "normal" way can be referred to as an instance variable.

class Cat {
   int weight;
}

Each time you create a new object of type Cat, you create a new copy of the variable 'weight'. If you create 10 objects of type Cat, each one has it's own copy of the weight variable.

A 'static' variable can be thought of as a class level variable, as opposed to an instance variable. A static variable has only one copy and belongs to the class Cat itself, rather than there being one copy for each object of type Cat.

class Cat {
   static String speciesName;

   int weight;
}

Here, no matter how many objects of type Cat we create, there is only one copy of speciesName.

If the static variable is also 'final,' than this one copy of the variable is the only piece of storage that cannot be changed. If the variable 'weight' were final in the above example, there would be 10 pieces of storage which could not be changed -- one for each object of type Cat that we had created.

Mike Edwards
  • 3,742
  • 17
  • 23
26

A static variable is common for all instances of the class. A final variable can not change after it has been set the first time.

So a static final variable in Java is common for all instances of the class, and it can not be changed after it has been set the first time.

class Car {

    static final int numberOfWheels = 4;
    Color color;

    public Car(Color color) {
        this.color = color;
    }
}

Car redCar = new Car(Red);
Car blueCar = new Car(Blue);

Each car now has one individual and variable property color and they share the property numberOfWheels which can not be changed.

Christoph Grimmer
  • 4,210
  • 4
  • 40
  • 64
Jonas
  • 121,568
  • 97
  • 310
  • 388
  • 2
    @Jonas: I would hesitate to call a `final` variable "constant," because that term is often used for compile-time variables or variables which are assigned when they are declared. Instead, I would describe a `final` variable as one which can only be assigned once (in the case of a class member this would need to be in the constructor). – Brian Oct 25 '10 at 21:10
  • 2
    @Brian I didn't see the original phrasing, but final variables are much like constants in Java. They compile into the code the way that a #define would, and have some interesting side-effects therefrom (such as changing the #define and recompiling that class only will NOT update the value in other classes--they will have the original value compiled in). What different definition of "Constant" did you have? – Bill K Oct 25 '10 at 21:35
  • @Bill K: That's interesting, I didn't know that. Oracle/Sun also call class variables for **Constants** on [Java Tutorials](http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html) – Jonas Oct 25 '10 at 21:42
  • @Bill K: I agree that a `static final` variable is effectively a constant. However, the original wording of Jonas's answer referred to a `final` variable as a constant, which I don't consider an accurate choice of phrasing, since different instances of the class could have different values for a final variable. – Brian Oct 25 '10 at 21:49
  • @Jonas: `static final` is basically a constant, as the link you provide indicates. However, `final` or `static` by itself do not create a constant variable. – Brian Oct 25 '10 at 21:53
  • 1
    @Jonas Actually that page you quoted is extremely accurate. It says nothing about class variables being constant, what it says about constants is: "The static modifier, in combination with the final modifier, is also used to define constants." and goes on to differentiate traditional "Compile-time" constants from constants calculated once at runtime. – Bill K Oct 26 '10 at 17:22
1
  • Final means cannot re-assign value to any variable
  • Static means only one copy of reference can be in whole in the class of all methods.
giri-sh
  • 6,934
  • 2
  • 25
  • 50
Vishwa
  • 11
  • 1
1

See the section 'Constants' for an explanation on this page:

http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html

rmk
  • 4,395
  • 3
  • 27
  • 32