About static leaf keywords in Java

According to the tutorial :

The static modifier in combination with the final modifier is also used to define constants. The final modifier indicates that the value of this field cannot be changed.

I agree with this only if the types were primitive. With reference types, for example. an instance of the Point2D class, where its position attributes were not final (i.e. we could change its position), attributes of this type of variables, such as public static final Point2D A = new Point2D(x,y); are still subject to change. It's true?

+45
java static constants final
Jan 16 '13 at 12:38
source share
15 answers

Yes, it can be changed. Only links cannot be changed, but there can be internal fields. The following code shows this:

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

Groovy (an alternative JVM ) has an annotation called @Immutable , which blocks the change in the internal state of an object after it is created.

+66
Jan 16 '13 at
source share

That's right, it can still be changed. "Static final", in this case, refers to the link itself, which cannot be changed. However, if the object that is the link is modified, then the object that it refers to can be modified.

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

+16
Jan 16 '13 at
source share
 public static final Point2D A = new Point2D(x,y); 

Here the link A final and not the values inside the Point2D class.

You cannot do this after defining a static ending:

 //somewhere else in code A = new Point2D(x1,y1); 
+10
Jan 16 '13 at
source share

public static final Point2D A = new Point2D (x, y); can still be changed. It's true?

Link A cannot be changed, but its true value as an object can be changed if the attributes are not final.

 class Point2D{ private int x; private int y; <getter & setter> } class Constant{ Public static final Point2D A = new Point2D(1,2); public static void main(String[] args){ A.setX(10); // this will work A = new Point2D(10,20);// this will not work } } 

In case Point2D's attributes are final, then the Point2D class will be immutable .

or you can send an object capable of cloning.

It seems -

  private static final Point2D A = new Point2D(x,y); public static getA(){ return A.clone(); } 
+6
Jan 16 '13 at
source share

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

This means that the link will not change. The content of the link (object field) may change over time.

+6
Jan 16 '13 at
source share

The reference to the point ( A in your case) cannot change. Only the state of an object can change. Thus, you cannot create a new Point2D and assign it to a variable.

+2
Jan 16 '13 at
source share

Only the link is final, the referenced object can be modified (unless it is an immutable object, such as Integer or the like). So yes, it is only constant for a given value of "constant".

+2
Jan 16 '13 at
source share

Yes.

Of course, you can also change the value of the final field later as described elsewhere .

+1
Jan 16 '13 at 18:50
source share

Here you can find a very similar question with a good explanation:

HERE: Why can the final object be modified?

By the way, I started thinking about the reflection mechanism ....

in theory ... I believe that you can get an instance of the owner class .. then get the members of the class, find the current instance and check its final ....

I have not tested it, and I can’t even figure out if this is possible - it’s just an idea (maybe?)

at

 public class Final { static final Point p = new Point(); public static void main(String[] args) throws MyImmutableException { p = new Point(); // Fails p.setB(10); // OK p.setA(20); // Fails - throws MyImmutableException } } public class Point() { int a = 10; int b = 20; public setA(int a) { this.a = a; } public setB(int b) throws MyImmutableException { detectIsFinal() this.b = b; } private void detectIsFinal() throws MyImmutableException { int mod = this.getClass().getModifiers() if (Modifier.isFinal(mod)) { throw new MyImmutableException(); } } } public class MyImmutableException extends Exception { public MyImmutableException() {super(); } } 

I thought what else you can do ... again it's just !!!!!!!!! PSEUDOCODE !!! I'm not sure if this will work: P

Perhaps someone who knows the annotations will be inspired and make it work. Unfortunately, this week I have no more time. Maybe later I will try to do POC.

 public class Final { @Immutable static final Point p = new Point(); public static void main(String[] args) { p = new Point(); // Fails p.setB(10); // Fails p.setA(20); // OK } } public class Point() { int a = 10; @ImmutableOnThisInstance int b = 20; @DetectIsImmutable public setA(int a) { this.a = a; } @DetectIsImmutable public setB(int b) { this.b = b; } } class Immutable { ????????????????? } class DetectIsImmutable { /** * Just a pseudocode - I don't know how to work with ANNOTATIONS :) :) :) * SORRY :) * @throws MyImmutableException */ private void detectMethod() throws MyImmutableException { Immutable instance = CLASS_THAT_IS_ANNOTATED.getClass().getAnnotation(Immutable.class) if (instance != null) { // get parent Method invocation name (from stacktrace list) String methodName = .............; if (methodName.startsWith("set")) { // check id we have variable with this settername String fieldName = ...; // cut "set" get rest of it, make first letterSmall // find this field in object fields Field f = this.getClass().getDeclaredField(fieldName); if (f.getAnnotation(ImmutableOnThisInstance.class) != null) { throw MyImmutableException(); } } } } } 
+1
Jan 22 '13 at 19:35
source share

This is still the case.

There is a way that the claim you quote is true. static final describes a variable, and it is true that this variable cannot change, and therefore is a constant. A variable is a pointer to an object, and this object can change. But this does not prevent the variable from being constant.

This is a less powerful way to be true, which can be achieved with const in C ++, but it is something.

0
Jan 16 '13 at
source share

Yes you are right. The link cannot be changed, but the referenced object can be. Something like this is completely legal:

 public static final List<Object> someList = new ArrayList<Object>(); // ... someList.add(someThing); // <-- this would be illegal if the referenced object was also constant 
0
Jan 16 '13 at
source share

You can change the properties of Point2D, but not create a new instance of it.

I should also point out that Point2D is abstract, so you must create an instance of a child class that extends it.

0
Jan 16 '13 at 12:43
source share

As already mentioned, the reference to your Point2D object is static, not the x and y attributes. If you want you to not be able to reposition the Point2D object, you must set the x and y attributes to be static and final (initialized) in the Point2D class.

0
Jan 16 '13 at 14:09
source share
 static final Point2D A = new Point2D(x,y); 

All he says is that the link of the Point2D class cannot be changed.

  _____________ | | A----------|--->(x,Y) | | | |_____________|Heap 

So you cannot change A by pointing to another object, but of course you can change the value (x, y) or the contents of a Point object

If you want to make your object also permanent, you have to make the Point object immutable.

0
Jan 29 '13 at 6:09
source share

When the reference variable is declared final, you cannot reassign it a new object when it refers to the object. But you can change the state of the object that the destination reference variable points to. Take a look at the example below.

 class A { int i = 10; } public class UseOfFinalKeyword { public static void main(String[] args) { final A a = new A(); //final reference variable ai = 50; //you can change the state of an object to which final reference variable is pointing a = new A(); //compile time error //you can't re-assign a new object to final reference variable } } 

For more information, follow the link: final keyword in java

0
Mar 21 '15 at 2:04
source share



All Articles