Interview kitsBlog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2026 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Java
Javaeasy

What does `final` mean for a variable, a method, and a class?

Tags
#final#inheritance#java-basics
Back to categoryPractice quiz

Answer

A `final` variable can’t be reassigned, a `final` method can’t be overridden, and a `final` class can’t be extended. (It does not automatically make an object immutable.)

Advanced answer

Deep dive

`final` means “cannot be changed in that dimension”, but the meaning depends on where it’s applied.

  • `final` local variable / parameter: you can’t reassign the reference.
  • `final` field: must be assigned once (constructor, initializer). The reference can’t change, but the object can still be mutable.
  • `final` method: prevents overriding (useful for API stability/security).
  • `final` class: prevents inheritance (e.g., `String`).

Concurrency note

Final fields have special visibility guarantees after construction (when the object is safely published). This is why immutable objects with final fields are easier to reason about.

Example

final List<String> xs = new ArrayList<>();
xs.add("a");      // ok (mutating object)
// xs = new ArrayList<>(); // not ok (reassign)

Common pitfalls

  • Confusing `final` reference with immutability.
  • Relying on `final` for thread-safety (it helps, but doesn’t make mutable state safe).

Related questions

Java
What are sealed classes in Java and why use them?
#java#sealed#inheritance
JavaScript
Explain prototypal inheritance in JavaScript.
#prototypes#inheritance