Do trailing fields really prevent volatility in Java?

From the famous Java book Concurrency in Practice Chapter 3.4.1 Final Fields

Just as it is good practice to make all fields private, unless they need more clarity [EJ Paragraph 12], it is good practice to make all fields final if they are not to be mutable.

My understanding of Java end links: the final reference / field simply prevents it from getting the field initialized, but if it refers to a mutable object, we can still change its state, turning it into mutable. Therefore, it is difficult for me to understand the above quote. What do you think?

+6
source share
5 answers

final fields do not allow you to change the field itself (making it a β€œpoint” for any 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 link p above is unchanged, but the actual person referenced by the link is volatile. you can, of course, make the Person class an immutable class, for example:

 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, when they were created, cannot be changed in any way.

+9
source

This quote says only what is said:

make all fields final if they should not be mutable

a modifiable field is a field that you can change later to point to another object. If the field is final , it can still refer to a mutable object (for example, java.util.Date ). Thus, the field is immutable (always pointing to the same object), but this object is mutable.

+3
source

in java final simulator:

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, the final fields cannot be changed, but the object to which they refer can change by itself. Ex. 'this' is final, you cannot assign anything to this , but you can assign to the object to which it relates.

From your questions:

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

to avoid any modifications (logically or accidentally) to the original, it is always useful to make them final.

+2
source

According to Java code conventions, final variables are considered constant and written in all Caps, for example.

 private final int COUNT=10; // immutable -> cannot be changed 

Creating a control variable for a collection link means that the link cannot be changed, but you can add, delete, or modify the object within the collection. For instance:

 private final List Loans = new ArrayList(); list.add("home loan"); //valid list.add("personal loan"); //valid loans = new Vector(); //not valid 

More details: http://javarevisited.blogspot.com/2011/12/final-variable-method-class-java.html#ixzz2KP5juxm0

To answer your question: Yes, if you use them correctly.

0
source

Do not pay attention to the quote. There is little benefit in having your code smoothed with final from time to time. The author strove for good practice, but in large codebases, seeing that everything made it completely obscure the intention, making reading the code more complex and more obscure - and can be of little use.

Two approaches

Generally, although there are two schools. Choose your option:

  • AGAINST if it is really necessary

One should not use final , unless you REALLY want those who read the code you wrote to know that this field is special and should not be confused.

  • For there are never bad places to use in the finals!

Another is in love with final and wants him everywhere. It is less common. Renauld Waldura makes a great promoter of ideas. Read his related entry, entitled β€œFinal Word by Final Keyword,” which is a good and in-depth reading.

May depend on the Java you are using.

For my part, I just want you to know that final changed and does NOT ALWAYS mean unmutable, as you can read in the Heinz Kabutz "Java Specialist Newsletter". He will guide you through how he works in different javas; feel free to take his code and check the Java you are using.

0
source

All Articles