Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do final fields really prevent mutability in Java?

From the famous book Java Concurrency in Practice chapter 3.4.1 Final fields

Just as it is a good practice to make all fields private unless they need greater visibility[EJ Item 12] , it is a good practice to make all fields final unless they need to be mutable.

My understanding of final references in Java : A final reference/ field just prevents the the field from getting re initialized but if it references a mutable object , we can still change its state rendering it mutable . So I am having difficulty understanding the above quote . What do you think ?

like image 452
Geek Avatar asked Feb 09 '13 12:02

Geek


People also ask

Are final variables mutable in Java?

The final Keyword in Java In Java, variables are mutable by default, meaning we can change the value they hold.

What is the advantage of using final keyword in Java?

Instance and Class Variables We can apply the final keyword to the instance or class variables. That way, we ensure that their value assignment can be done only once. We can assign the value upon final instance variable declaration, in the instance initializer block or in the constructor.

Is using final good practice Java?

Consistently using final with local variables (when appropriate) can be useful as well. It brings attention to the non- final local variables, which usually have more logic associated with them (for example, result variables, accumulators, loop variables). Many find this verbose.

Does all property of immutable object needs to be final?

No, it is not mandatory to have all properties final to create an immutable object. In immutable objects you should not allow users to modify the variables of the class. You can do this just by making variables private and not providing setter methods to modify them.


3 Answers

final fields prevent you from changing the field itself (by making it "point" to some other instance), but if the field is a reference to a mutable object, nothing will stop you from doing this:

public void someFunction (final Person p) {
    p = new Person("mickey","mouse"); //cant do this - its final
    p.setFirstName("donald");
    p.setLastName("duck");
}

the reference p above is immutable, but the actual Person pointed to by the reference is mutable. you can, of course, make class Person an immutable class, like so:

public class Person {
    private final String firstName;
    private final String lastName;
    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
    //getters and other methods here
}

such classes once created, cannot be modified in any way.

like image 110
radai Avatar answered Oct 23 '22 02:10

radai


This quote says only what is says:

make all fields final unless they need to be mutable

mutable field is a field that you can later change to point to another object. If the field is final, it can still reference mutable object (e.g. java.util.Date). Thus the field is immutable (always points to the same object), but this object is mutable.

like image 37
Tomasz Nurkiewicz Avatar answered Oct 23 '22 02:10

Tomasz Nurkiewicz


in java final is similer to this:

int *const p=&something //constant pointer.in c++. you can't use this pointer to change something but that something can be changed with its own.

in java final fields can't be changed but the object to who they refer may change by its own . Ex. 'this' is final you can't assign anything to this but you can assign to object to whom this refer.

From your Ques:

it is a good practice to make all fields final unless they need to be mutable.

to just avoid any modification(either logically or accidently) to original it's always good to make them final.

like image 2
Arpit Avatar answered Oct 23 '22 02:10

Arpit