2

According to this: A Strategy for Defining Immutable Objects

One of the conditions for a class to be immutable, is making all its fields final and private.

Why final??? The other conditions aren't sufficient?

Hicham Moustaid
  • 773
  • 6
  • 13
  • Read carefully: _Not all classes documented as "immutable" follow these rules._ – Matt Ball May 20 '15 at 03:51
  • 2
    `final` has implications for thread-safety - see this blog post: http://jeremymanson.blogspot.co.nz/2008/04/immutability-in-java.html – Blorgbeard May 20 '15 at 03:56
  • Can `Class.getDeclaredField().set...` be used to change the value of a final field? I know it can be used to change the value of a private field. – Paul Hicks May 20 '15 at 04:32

4 Answers4

1

Without making the field final we can make an immutable class/object if other conditions are available.

But I think the final is useful while dealing with concurrency and synchronization.

Razib
  • 10,965
  • 11
  • 53
  • 80
1

Per the definition for an immutable object (courtesy of Wikipedia) "In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created."

Once an final object has been created it cannot be re-assigned. Without the final key work you could still change an object after it has been created.

See also final object in java

Community
  • 1
  • 1
  • If the class is final and its fields are private and no setters are defined and we don't share references to the mutable objects (via getters for example), the class is immutable...these conditions are sufficient (no need for the fields to be final). – Hicham Moustaid May 20 '15 at 04:39
  • Why not let the compiler check that for you? – keuleJ May 20 '15 at 05:19
  • I appear to be having some difficulty with the enter key tonight There are different levels of immutable which may or may not use final, I will not even pretend like I understand them all. An example of why the other conditions alone are not sufficient “in certain cases” from Oracles documentation: “final fields also allow programmers to implement thread-safe immutable objects without synchronization.” http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.5-110 For a probably much better answer then my own see assylias response in http://stackoverflow.com/questions/16061030/ – Trace Moridin May 20 '15 at 06:34
1

Counter question "Why not final?".

final means for primitive types you'll not be able to change the value once assigned which is enough to make them Immmutable,

while for non-primitive types the reference can't be changed (1st step towards Immutability) once assigned and you need to do some more as mentioned in the link shared by you.

Nitin Dandriyal
  • 1,559
  • 11
  • 20
0

The key to the linked document is this quote

Not all classes documented as "immutable" follow these rules....However, such strategies require sophisticated analysis and are not for beginners.

This is a tutorial for beginners. It's easier to tell them "make everything private and final" then have to explain all the edge cases with how to properly handle mutable references and making sure not to let your references escape.

dkatzel
  • 31,188
  • 3
  • 63
  • 67