The ultimate variable manipulation in Java

Can someone please tell me what the following line means in the context of Java:

the final variable can still be manipulated if it is not immutable

As far as I know, declaring a variable to be final, you cannot change it again, and then what do the words immutable in the line above mean?

+44
java variables final
Aug 08 '09 at 21:14
source share
9 answers

This means that if your final variable is a reference type (i.e. not primitive like int), then this is just a reference that cannot be changed. You cannot reference another object, but the fields of the object to which it relates can still be changed if the class allows it. For example:

final StringBuffer s = new StringBuffer(); 

The contents of a StringBuffer can still be changed arbitrarily:

 s.append("something"); 

But you cannot say:

 s = null; 

or

 s = anotherBuffer; 

On the other hand:

 final String s = ""; 

Strings are immutable - there simply is no method that would allow you to modify String (unless you use Reflection - and go to hell).

+94
Aug 08 '09 at 21:19
source share

If you have a final reference to a Java object, you can still manipulate it, but you cannot change its reference. For example, this code is completely legal:

 import javax.swing.JLabel; class Test1 { private final static JLabel l = new JLabel("Old text"); public static void main(String[] args) { System.err.println(l.getText()); l.setText("New Text"); System.err.println(l.getText()); } } 

But you cannot say:

 l = new JLabel("Newest Text"); 

After the first assignment l. Please note that you can do this though:

 import javax.swing.JLabel; class Test1 { public static void main(String[] args) { final JLabel l; String s = getArbitaryString(); // Assume this method returns a string l = new JLabel(s); System.err.println(l.getText()); } } 

This can be done because when l is declared, it is not assigned to anything not even null. Thus, you are allowed to assign him something only once.

The same goes for primitives. You can assign it a value as follows:

 class Test1 { public static void main(String[] args) { final int i; i = 2; } } 

But now you cannot manipulate it further, because the only thing you can do for primitive types is to assign values ​​to them.

+16
Aug 08 '09 at 21:17
source share

You cannot change which object or value the final variable belongs to. You can only assign the final variable once.

This does not affect the change in state of the object. The object itself can still be manipulated if it is not encoded in such a way that this manipulation is prohibited. An immutable object is an object whose state cannot change.

+4
Aug 08 '09 at 21:20
source share

As others have said, this means that you can manipulate the object that the variable points to, but you cannot change the link (i.e. assign another object to the variable).

Objects that change in design, such as List , can be changed (you can add elements to them), whereas if you have an immutable object, such as String or Integer , you cannot change it (all operations supported by the class String , return a new instance and do not modify the actual object).

+3
Aug 08 '09 at 21:20
source share

You can call any method on it, even if the method can change the state of the object referenced by. for example

 final MyClass myClass = new MyClass(); myClass.setVar(something); 

This is great because myClass itself myClass not change, i.e. you do not execute myClass = myClass1; .

0
Feb 27 2018-10-28
source share

The one that always kills me?

If you want the final variables to be really as safe as you thought, you need a lot of extra code to return a copy of the string [].

0
Feb 27 '10 at 2:51 p.m.
source share

You can manipulate mutable final variables, for example. of type StringBuffer, but you cannot manipulate the final variables of immutable types.

In the case of mutable variables, a new object is not created each time the value changes. But in the case of immutable types, when you change the value, a new object is created, so when you make it final, you cannot change it.

0
Aug 23 '12 at 18:24
source share

Yes, the final variable can be changed.

  final StringBuffer s = new StringBuffer(); // won't work s = new StringBuffer(); //this works s.append("hai"); 

You cannot change the link, but you can change the fields of the object. for more details

0
Feb 23 '15 at 4:47
source share

You can still change the "final" variable using Reflection.

-2
Aug 08 '09 at 21:17
source share



All Articles