What does final do?
Options
- Variable: assign once. Method: no override. Class: no extends (Correct option)
- It deletes the object at the end of the method call
- It makes a variable global, so every class can change it
- It marks the last line that the program will ever run
Answer
On a variable: it can be assigned only once. On a method: it cannot be overridden. On a class: it cannot be extended
Why
final means "cannot be changed or replaced", and it applies differently:
- final variable: assigned once. For an object, the reference cannot point elsewhere, but the object's inside can still change (a
final Listcan still receiveadd). - final method: subclasses cannot override it.
- final class: nobody can extend it (
Stringis final).
Remember
final = no reassign, no override, no extend.