On the static final keywords in Java

Solution 1:

Yes, it can be changed. Only the references cannot be changed, but its internal fields can be. The following code shows it:

public class Final {
    static final Point p = new Point();
    public static void main(String[] args) {
        p = new Point(); // Fails
        p.b = 10; // OK
        p.a = 20; // Fails
    }
}

class Point {
    static final int a = 10;
    static int b = 20;
}

Groovy (an alternative JVM language) has an annotation called @Immutable, which blocks changing to a internal state of an object after it is constructed.

Solution 2:

Correct, it can still be changed. The "static final", in this case, refers to the reference itself, which cannot be changed. However, if the object that it is references is mutable, then the object that it references can be changed.

An immutable object, such as a String, will be a constant.

Solution 3:

public static final Point2D A = new Point2D(x,y);

Here the reference A is final and not the values inside the class Point2D.

You cannot do this after defining A static final:

//somewhere else in code
A = new Point2D(x1,y1);

Solution 4:

It's true. The final modifier is not transitive in any possible way.

It only means the reference will not change. The content of the reference (the fields of the object) may change over time.