A final reference to a mutable object
final means 'can't be changed after it's set': a final variable can be assigned only once, a final method can't be overridden, and a final class can't be extended. A final reference can't point elsewhere, but the object it points to can still change.
Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write an immutable Money class: final class, private final amount (long paise) and currency, no setters, a plus(Money) that returns a new object. Show the original is unchanged.
Show with code that a final StringBuilder can still be appended to, but not reassigned.
Write a class with a blank final id that has two constructors, both assigning it, and print both objects.
Explain it without notes
What does final mean on a variable, a method and a class?
Does final List<String> list make the list immutable? Explain.
What is a blank final, and what does the compiler check?
What is 'effectively final' and where does it matter?
Expected output
[Asha, Ravi]
[99, 2, 3]
frozen list refused add()
label = VIP