Java Tutorial
Java final
final locks a variable to one assignment, a method against overrides, or a class against subclasses — three related ideas, one keyword.
final variables
Mark a variable final when it must never be reassigned after its first assignment. The compiler enforces it — ideal for constants, fixed configuration, and values you want to treat as read-only.
Example
public class Main {
public static void main(String[] args) {
final double TAX = 0.2;
double price = 50.0;
System.out.println(price * (1 + TAX));
// TAX = 0.25; // error: cannot assign a value to final variable
}
}final references vs mutable objects
A final reference cannot point somewhere else, but the object it points to may still change — unless that object is itself immutable.
Example
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
final List names = new ArrayList<>();
names.add("Ada"); // OK — list contents change
// names = new ArrayList<>(); // error — cannot reassign final
System.out.println(names);
}
} Class constants are usually public static final and named inUPPER_SNAKE_CASE: public static final int MAX = 100;.
final methods
A final method cannot be overridden in a subclass. Use it when a piece of behavior must stay exactly as written in the parent.
Example
class Account {
final String id() {
return "ACC";
}
}
class Savings extends Account {
// String id() { return "SAV"; } // error: cannot override final method
}
public class Main {
public static void main(String[] args) {
System.out.println(new Savings().id());
}
}final classes
A final class cannot be extended. String is final for that reason — the platform guarantees its behavior. Your own domain types can be final when inheritance would only invite misuse.
Example
final class Token {
final String value;
Token(String value) { this.value = value; }
}
// class FakeToken extends Token {} // error: cannot inherit from final class
public class Main {
public static void main(String[] args) {
System.out.println(new Token("abc").value);
}
}final on a field of a reference type does not freeze the object’s interior — only the reference. For true immutability, use unmodifiable collections or records with immutable components.
Try It Yourself
Exercise: Declare a final int MAX_SCORE = 100 and print whether a variable score is within range (0 to MAX_SCORE inclusive).
Show solution
final int MAX_SCORE = 100;
int score = 87;
boolean ok = score >= 0 && score <= MAX_SCORE;
System.out.println(ok);MAX_SCORE cannot be reassigned, so the upper bound stays fixed for the rest of the method.
Key Takeaways
finalvariables get one assignment; reassignment is a compile error.- A final reference can still point at a mutable object.
finalmethods cannot be overridden;finalclasses cannot be subclassed.- Use
static finalUPPER_SNAKE constants for shared fixed values.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Physics
Planck constant once
final marks a value you must not reassign. Physical constants belong here so a later line cannot overwrite h.
E = hf
Example
public class Main {
public static void main(String[] args) {
final double h = 6.626e-34;
double f = 5.00e14;
System.out.println(h * f);
}
}