Command Palette

Search for a command to run...

Back to the lesson: Topic 5.10 — The final Keyword
Core Java · Example 2 of 3

Blank finals, constants and a final template

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.

01

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.

02

Show with code that a final StringBuilder can still be appended to, but not reassigned.

03

Write a class with a blank final id that has two constructors, both assigning it, and print both objects.

Explain it without notes

01

What does final mean on a variable, a method and a class?

02

Does final List<String> list make the list immutable? Explain.

03

What is a blank final, and what does the compiler check?

04

What is 'effectively final' and where does it matter?

Blank finals, constants and a final template
Sign in to run this example in your browser.

Expected output

Order 1: 100.00 + tax = 118.00
Order 2: 100.00 + tax = 106.20
large