1

I understand that it is a good practice to use final for object fields that are 1) set only once in the constructor 2) reference to an immutable object.

What about fields that are set only once in the constructor but reference to a mutable object?

Currently, I prefer using final only for immutable object references, because I can quickly see which fields represent the mutable state of the object (those that are not final).

Update: I know how final works technically, specifically that setting a reference to a mutable object as final won't make the object immutable. This question is about when exactly use final if I want to maximize clarity and understandability of the code.

fhucho
  • 34,062
  • 40
  • 136
  • 186
  • @Kylo sorry, I don't understand, why do I think what? – fhucho Dec 10 '12 at 20:08
  • 1
    It's up to you really. I use `final` on fields whenever possible (even if they're mutable - typically collections) because it still shows _intent_. – Dmitri Dec 10 '12 at 20:11
  • @fhucho: this second point is not entirely true and I am wondering why you think that only immutable objects should have final reference. – Kylo Dec 10 '12 at 20:12
  • @Kylo that's what I'm asking - if only references to immutable objects should be final or if I should use final whenever possible. – fhucho Dec 10 '12 at 20:20
  • 1
    If you have a mutable reference to a mutable, that gets confusing very fast. Look at the setModels in Swing - typically this fails if the component is live at the time. Would have been better to remove the method and only have one shot a specifying the model during construction. – Tom Hawtin - tackline Dec 10 '12 at 20:53
  • @TomHawtin-tackline I think that definitely depends on the situation. An implementation of a linked list would be very strange/difficult, I think, if everything wasn't mutable. – Jeff Dec 10 '12 at 20:58
  • @Jeff I think (just intuition) that immutable linked list shouldn't be hard to implement. What exactly would be difficult? – fhucho Dec 10 '12 at 21:18
  • @fhucho This should be moved to chat, but I'm not entirely sure how, so I'll reply here anyway: This could just be that I'm a bit of a rookie, but the way I've always known linked lists to be implemented they have (at minimum) a forward link and a value. The value should be immutable, but the link shouldn't, and the same is true for all links. Thus you have a chain of mutable links to (at least partially) mutable objects. – Jeff Dec 10 '12 at 21:23
  • @Jeff Linked list is the canonical example of something which is a bit tricky to deal with. (If you really want, you can construct immutable doubly-linked lists.) – Tom Hawtin - tackline Dec 10 '12 at 23:46
  • @TomHawtin-tackline Sure, and I'm aware that there are use cases for that (e.g. for synchronization issues) but my point is that there are also reasonable use cases where mutable references to mutable objects are reasonable - something I still feel that linked lists show - they're not so confusing that they simply shouldn't be used. – Jeff Dec 10 '12 at 23:53
  • 1
    @Jeff I wasn't suggesting it shouldn't be done. In the bigger picture it's done all the time. I'm merely suggesting limiting mutability, rather than saying "right I want some mutability here, so let's remove all the immutability switches". I think we're in agreement. – Tom Hawtin - tackline Dec 11 '12 at 00:06

2 Answers2

2

This might not be a popular opinion, but I use final wherever possible - if you're using it to declare intent, then that can be done with comments. If your reaction to that is that "comments are bad", then you should reconsider the use of the final keyword also - as it's basically a comment, right?

However, the final keyword does provide a (almost) guarantee to anyone reading the code that that variable isn't reset anywhere (ignoring reflection of course...) - which is a useful thing to know at times.

You might also be interested in Jon's answer to this question

EDIT: Sorry, I should clarify that "wherever possible" applies to fields - not classes, methods etc. because that would be weird.

Community
  • 1
  • 1
Jeff
  • 12,555
  • 5
  • 33
  • 60
1

Even if you declare the variable final the referenced object will still be mutable. So if this is what you are trying to avoid then it would not work (I mean make the referenced object immutable)

Cratylus
  • 52,998
  • 69
  • 209
  • 339