Final Keyword in Java — Java Technical Interview Questions & Answers
Final Keyword in Java
Final Keyword in Java
- final means do not change this further — but the exact meaning depends on where you put it. final variable: cannot reassign after it is set. final method: child cannot override it. final class: nobody can extend it.
- Common pattern: final int MAX = 100;. If the variable holds an object, the reference is fixed. Fields inside that object can still change unless the object itself is immutable.
- Explain all three in three lines in an interview: variable, method, class.
- Let's take this on the board with one tiny Main class — no extra files. Final Keyword in Java — output: 100. Uncomment MAX = 200 and javac fails — final cannot be reassigned. Start from main, go line by line, and stop at each print. That output is the proof for Final Keyword.
Technical Interview Tip: Three meanings of final — variable, method, class.
Code Examples for Final Keyword in Java
Example
public class Main {
public static void main(String[] args) {
final int MAX = 100;
System.out.println(MAX);
// MAX = 200; // compile error
}
}Final Keyword in Java — output: 100. Uncomment MAX = 200 and javac fails — final cannot be reassigned.
How to Explain Final Keyword in Java to an Interviewer
What does final variable mean?
Cannot reassign
What does final method mean?
Child cannot override
What does final class mean?
Nobody can extend it
Does final object freeze all fields?
No Reference is fixed State may still change