views:

141

answers:

3

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?

+12  A: 

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;
    }
}

Now these cars have one variable color each, but they share the variable numberOfWheels and it can not be changed.

Car redCar = new Car(Red);
Car blueCar = new Car(Blue);
Jonas
@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
@Brian: You are right about that. I have updated my answer.
Jonas
+1 good answer!
Erick Robertson
@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
@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
@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
@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
@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
+1  A: 

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

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

rmk
+1  A: 

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