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

Updated 16 Nov 20266 min read
final Keyword in Java: Constants, Inheritance Limits and Restrictions

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, final permits at most one assignment.

  • On an instance method, final prevents overriding.

  • On a class, final prevents subclassing.

For a primitive, the stored value is fixed after assignment:

java
final int attempts = 3;
attempts = 4; // compile-time error

For an array reference, the identity of the referenced array is fixed, but its elements can still change:

java
final int[] scores = {40, 60};
scores[0] = 45;                 // legal
scores = new int[]{50, 70};     // compile-time error

Position

What is fixed

Still allowed

Rejected

final int

Primitive value after assignment

Reading and using the value

A second assignment

final int[]

Array reference after assignment

Mutating an array element

Pointing the variable to another array

final method

Its inherited implementation

Inheriting and calling it

Overriding it

final class

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:

java
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.

java
final String id;
if (registered) {
    id = "KG-17";
}
System.out.println(id); // compile-time error: id may be unassigned

Putting 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:

java
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:

  1. The constructor assigns the blank final field accountId once to "KG-17".

  2. It assigns monthEndBalances once to a reference containing [8000, 12000].

  3. The statement at index 1 mutates the existing array, producing [8000, 15000]. It does not reassign the reference.

  4. StudentAccount inherits bonusForMonth. The call uses the updated value and computes 15000 * 10 / 100 = 150000 / 100 = 1500.

The output is exactly:

Code
KG-17
1500

Four related operations fail at compile time, not at runtime:

  • account.accountId = "KG-18"; tries to reassign a final field.

  • account.monthEndBalances = new int[]{9000, 16000}; tries to reassign a final reference.

  • Declaring int bonusForMonth(int monthIndex) in StudentAccount tries to override a final method.

  • class CustomPolicy extends LockedPolicy {} tries to subclass a final class.

StudentAccount trace: KG-17 cannot become KG-18, a final array reference cannot change, and inherited bonusForMonth returns 1

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.

Inheritance diagram showing StudentAccount inheriting bonusForMonth from SavingsAccount, the rejected override, and CustomPolicy blocked from extending final class LockedPolicy.

5. Restrictions and combinations the compiler rejects

Declaration

Legal?

Reason

final void close()

Yes

An instance method may block overriding.

abstract final void close()

No

An abstract method requires an overriding implementation, while final forbids one.

final Example()

No

A constructor cannot be final.

final volatile int state = 0

No

A field cannot be both final and volatile.

Interface field int LIMIT = 100

Yes

An interface field is implicitly public static final.

Finality remains shallow for mutable objects:

java
final StringBuilder label = new StringBuilder("KG");
label.append("AI");
System.out.println(label.toString()); // KGAI

label = new StringBuilder("New");     // compile-time error

The 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:

  1. A final reference does not necessarily point to an immutable object.

  2. A final method can be inherited, although it cannot be overridden.

  3. A final class may still inherit from its own parent.

  4. A static final field is not necessarily a compile-time constant.

  5. A blank final field 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 final primitive allows no second assignment.

  • A final reference allows no new referent, but the object may remain mutable.

  • A constant variable needs a primitive or String type and a constant-expression initializer.

  • A blank final needs definite assignment exactly once.

  • An accessible final method can be inherited but not overridden.

  • A final class cannot be subclassed.

Consider this prediction:

java
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.