Reputation: 27199
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 field from getting reinitialized 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 ?
Upvotes: 9
Views: 8134
Reputation: 12797
In Java, final is similar 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 which the refer may change by its own. For example, this
is final, you can't assign anything to this
, but you can assign to object to which this refers.
From your question:
it is a good practice to make all fields final unless they need to be mutable.
To just avoid any modification (either logically or accidentally) to original, it's always good to make them final.
Upvotes: 2
Reputation: 340923
This quote says only what is says:
make all fields final unless they need to be mutable
A 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.
Upvotes: 4
Reputation: 24202
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.
Upvotes: 18
Reputation: 2924
Disregard the quote. There's little to no benefit from making your code blotted with final
's every now and then. Author aimed for a good practice, but in large code-bases, seeing everything made final rather obscures the intent, making reading code harder and more obscure - and may give little to no benefit.
Generally though, there are two schools. Choose yours:
One is NOT to use final
, unless you REALLY want those reading the code you wrote to know that this field is special in some way and is not to be trifled with.
The other is enamored with final
and wants it everywhere. It's less prevalent. Renauld Waldura makes for an excellent propagator of the idea. Read his linked entry, titled "Final word on final keyword", it's a good and in-depth read.
On my part I'd just want you to know that final
changed, and doesn't ALWAYS mean unmutable, as you can read in Heinz Kabutz' Java Specialist Newsletter. He takes you through how it worked in different Javas, feel free to take his code and check the Java you use.
Upvotes: -1
Reputation: 3716
As per Java code convention final variables are treated as constant and written in all Caps e.g.
private final int COUNT=10; // immutable -> cannot be changed
Making a collection reference variable final means only reference can not be changed but you can add, remove or change object inside collection. For example:
private final List Loans = new ArrayList();
list.add(“home loan”); //valid
list.add("personal loan"); //valid
loans = new Vector(); //not valid
Read more: http://javarevisited.blogspot.com/2011/12/final-variable-method-class-java.html#ixzz2KP5juxm0
To answer your question: Yes, if you use them correctly.
Upvotes: -1