final Keyword in Java: Constants, Inheritance Limits and Restrictions
Learn what Java's final keyword fixes in each declaration position, then trace assignment, array mutation, method inheritance, arithmetic, and compiler errors.
KnowledgeGate Team
Exam prep & CS education

The final keyword appears beside variables, methods, classes, fields and parameters, but it does not make all of them constant in the same sense. In the worked StudentAccount example, accountId cannot change, an array element moves from 12000 to 15000, an inherited final method returns 1500, an override is rejected, and a final class cannot be extended. Read final by declaration position, then identify the operation Java prevents.
static answers who owns a member; final answers which change is forbidden. Static and Final in Java: Shared State, Constants and Override Rules with Runnable Examples traces shared counters and class-initialisation order. Compile-time constants, blank finals, reference mutation, non-overridable methods and non-extendable classes require a separate position-by-position test.
1. Read final by where it appears
On a variable, field, parameter, or reference,
finalpermits at most one assignment.On an instance method,
finalprevents overriding.On a class,
finalprevents subclassing.
For a primitive, the stored value is fixed after assignment:
final int attempts = 3;
attempts = 4; // compile-time errorFor an array reference, the identity of the referenced array is fixed, but its elements can still change:
final int[] scores = {40, 60};
scores[0] = 45; // legal
scores = new int[]{50, 70}; // compile-time errorPosition | What is fixed | Still allowed | Rejected |
|---|---|---|---|
| Primitive value after assignment | Reading and using the value | A second assignment |
| Array reference after assignment | Mutating an array element | Pointing the variable to another array |
| Its inherited implementation | Inheriting and calling it | Overriding it |
| Its place as the last class in that branch | Creating objects and inheriting from its own parent | Creating a subclass of it |
This position-first method also fits naturally into structured Java and DSA learning.
2. A final variable is not automatically a compile-time constant
A Java constant variable has a narrow definition: it is a final variable of primitive type or String type, initialised with a constant expression. Consider these exact declarations:
static final int BONUS_PERCENT = 2 * 5;
static final Integer BOXED_PERCENT = 10;
static final int PARSED_PERCENT = Integer.parseInt("10");BONUS_PERCENT is a constant variable with value 10. BOXED_PERCENT is final, but it is not a constant variable because its type is Integer, not a primitive type or String. PARSED_PERCENT is also final, but its initializer is a method call, so it is not a compile-time constant. The word static alone does not create a constant.
A blank final has no initializer at its declaration. For example, final String accountId; may be assigned in a constructor, but every constructor path must assign it exactly once before use. A blank static final field may be assigned in a static initializer. A blank final local variable must be assigned exactly once before it is read.
final String id;
if (registered) {
id = "KG-17";
}
System.out.println(id); // compile-time error: id may be unassignedPutting final on a parameter prevents reassignment only inside that method. In void add(final List<Integer> marks), assigning marks = new ArrayList<>() is rejected, while marks.add(80) can be legal. The caller is unaffected, and the list itself has not become immutable.
3. Worked example: trace assignment, mutation, inheritance and arithmetic
This runnable program brings the rules together:
class SavingsAccount {
static final int BONUS_PERCENT = 10;
final String accountId;
final int[] monthEndBalances;
SavingsAccount(String accountId, int janBalance, int febBalance) {
this.accountId = accountId;
this.monthEndBalances = new int[]{janBalance, febBalance};
}
final int bonusForMonth(int monthIndex) {
return monthEndBalances[monthIndex] * BONUS_PERCENT / 100;
}
}
class StudentAccount extends SavingsAccount {
StudentAccount() {
super("KG-17", 8000, 12000);
}
}
final class LockedPolicy {
static final int MAX_BONUS = 1500;
}
public class FinalDemo {
public static void main(String[] args) {
StudentAccount account = new StudentAccount();
account.monthEndBalances[1] = 15000;
System.out.println(account.accountId);
System.out.println(account.bonusForMonth(1));
}
}Trace it in execution order:
The constructor assigns the blank
finalfieldaccountIdonce to"KG-17".It assigns
monthEndBalancesonce to a reference containing[8000, 12000].The statement at index
1mutates the existing array, producing[8000, 15000]. It does not reassign the reference.StudentAccountinheritsbonusForMonth. The call uses the updated value and computes15000 * 10 / 100 = 150000 / 100 = 1500.
The output is exactly:
KG-17
1500Four related operations fail at compile time, not at runtime:
account.accountId = "KG-18";tries to reassign afinalfield.account.monthEndBalances = new int[]{9000, 16000};tries to reassign afinalreference.Declaring
int bonusForMonth(int monthIndex)inStudentAccounttries to override afinalmethod.class CustomPolicy extends LockedPolicy {}tries to subclass afinalclass.

4. Inheritance limits: a final method can be inherited
The central wording trap is to say that a final method is not inherited. In the program, StudentAccount legally extends SavingsAccount, inherits bonusForMonth, and calls it to get 1500. It simply cannot provide a different implementation with the same override signature. Inheritance in Java: Learn extends, super, and Overriding with Runnable Examples owns constructor chains, access rules, super, and runtime dispatch; the narrower rule here is that an accessible final method is inherited but cannot be overridden.
The class rule works in the other direction. final class LockedPolicy may itself extend a superclass that is not final, or implement interfaces, but no class may extend LockedPolicy. A final class still inherits eligible members from its own superclass. Therefore, "final means no inheritance" is too broad. The declaration position and direction matter.
Constructors cannot be final because constructors are neither inherited nor overridden. A private method is not an override target, so final is not a substitute for private. Fields are hidden rather than polymorphically overridden, which means a final field is mainly governed by assignment rules, not method dispatch.

5. Restrictions and combinations the compiler rejects
Declaration | Legal? | Reason |
|---|---|---|
| Yes | An instance method may block overriding. |
| No | An abstract method requires an overriding implementation, while |
| No | A constructor cannot be |
| No | A field cannot be both |
Interface field | Yes | An interface field is implicitly |
Finality remains shallow for mutable objects:
final StringBuilder label = new StringBuilder("KG");
label.append("AI");
System.out.println(label.toString()); // KGAI
label = new StringBuilder("New"); // compile-time errorThe reference stays fixed, while the same builder changes its contents from "KG" to "KGAI". The keyword alone does not make a collection unmodifiable, provide thread safety, or freeze the complete object graph.
Keep adjacent terms separate too. final controls declarations, finally belongs to exception-handling structure, and finalize names a different library concept. Similar spelling does not give them a shared rule.
6. Traps and common question formats
Use this five-item correction checklist:
A
finalreference does not necessarily point to an immutable object.A
finalmethod can be inherited, although it cannot be overridden.A
finalclass may still inherit from its own parent.A
static finalfield is not necessarily a compile-time constant.A blank
finalfield must be assigned on every constructor path, exactly once.
Typical questions remain answerable if you identify the declaration position first. An output prediction for the worked program gives KG-17 and 1500. A line-selection question asks why monthEndBalances[1] = 15000 compiles while assigning a new array does not. A compile-error question may test overriding bonusForMonth, extending LockedPolicy, or declaring abstract final. In every case, name the position, then state the exact restriction.
7. Short version and the next practice step
A
finalprimitive allows no second assignment.A
finalreference allows no new referent, but the object may remain mutable.A constant variable needs a primitive or
Stringtype and a constant-expression initializer.A blank
finalneeds definite assignment exactly once.An accessible
finalmethod can be inherited but not overridden.A
finalclass cannot be subclassed.
Consider this prediction:
final int[] values = {3, 5};
values[0] = 7;
System.out.println(values[0] + values[1]);The output is 12 because the contents become [7, 5], and 7 + 5 = 12. Replacing values with new int[]{1, 2} would be a compile-time error because that would change the reference identity. Explain both results aloud before moving on.
Use Complete Java for the full language sequence, or Java Programming for concept, MCQ, and coding practice.
Keep learning
Static and Final in Java: Shared State, Constants and Override Rules with Runnable Examples
Learn what static and final control in Java by tracing a shared counter, class initialisation, final references, constants and inheritance errors.
Inheritance in Java: Learn extends, super, and Overriding with Runnable Examples
Follow one runnable Vehicle and ElectricCar program to see inherited methods, constructor order, super calls, runtime dispatch, common errors, and practice tasks.
OOPs Concepts in Java: Inheritance, Polymorphism, Abstraction and Encapsulation with Worked Interview Questions
Turn the four OOP pillars into predictable Java behaviour. Trace superclass references, subclass objects, overridden methods, hidden fields and common follow-ups.
Object-Oriented Programming: Classes, Objects and Inheritance with a Worked Java Example
Trace two Java objects from construction to output, and see how independent state, inheritance and runtime method dispatch work together.





