01 / 20

Final Constants
in Java

Learn how to declare, name, and use final constants to write safer, more readable, and maintainable Java code.

Java AP Computer Science A

What is a Constant?

A constant is a variable whose value is assigned once and never changes during the program's execution. Think of it as a locked container: once you put something in, the lid is sealed forever.

Variable
Value can be updated at any time after initialization.
Constant
Value is locked after first assignment: it cannot be changed.

The final Keyword

In Java, you create a constant by adding the keyword final before the variable declaration. Once assigned, any attempt to change the value causes a compile-time error.

final double TAX_RATE = 0.08; // TAX_RATE = 0.10; ← compile error!

Why Use Constants?

Readability
A name like MAX_SPEED is clearer than the raw number 120.
Safety
The compiler prevents accidental re-assignment, eliminating a whole class of bugs.
Maintainability
Change one declaration instead of hunting for every occurrence of a magic number.

Syntax Rules

final dataType CONSTANT_NAME = value;
1
Keyword: Always place final before the data type.
2
Data type: Any valid type: int, double, String, etc.
3
Name convention: Use UPPER_SNAKE_CASE for all constant names.
4
Initialization: Assign a value immediately or in the constructor (for instance constants).

Anatomy of a final Declaration

final int MAX_STUDENTS = 30 ; Keyword Data Type UPPER_SNAKE_CASE Value (immutable) Locked

final vs Non-final Variables

final Variable Regular Variable
Re-assignment ✕ Compile error ✓ Allowed
Naming convention UPPER_SNAKE_CASE camelCase
Intent Value is a fixed fact Value will change
Bug protection High None (can change anywhere)
Typical use \( \pi \), tax rates, config Counters, accumulators

Local vs Class-Level Constants

Local Constant
Declared inside a method. Only accessible within that method's scope.
Class-Level Constant
Declared with static final at the class level. Shared across all instances.
// Class-level (shared, one copy in memory) public static final double PI = 3.14159; public void calculate() { // Local constant (method scope only) final int SIDES = 6; }

How to Apply Final Constants

1
Identify the fixed value: Is this number or string a fact that won't change? (tax rate, gravity, app name, etc.)
2
Choose the scope: Need it everywhere? Use static final at class level. Only in one method? Use local final.
3
Name it clearly: Use UPPER_SNAKE_CASE with a descriptive name: MAX_RETRIES, not MR.
4
Assign immediately: Give it a value on the same line (or in the constructor for instance finals).
5
Replace magic numbers: Search your code for raw literals and swap them with the constant name.

Example 1: Circle Area

Calculate the area of a circle with radius 5 using \( A = \pi r^{2} \). Instead of writing 3.14159 directly, we use a final constant.

public class CircleArea { public static final double PI = 3.14159; public static void main(String[] args) { final int RADIUS = 5; double area = PI * RADIUS * RADIUS; System.out.println(area); // 78.53975 } }

Example 2: Sales Tax Calculator

public class TaxCalc { public static final double TAX_RATE = 0.075; public static final String CURRENCY = "USD"; public static double totalWithTax(double price) { return price + (price * TAX_RATE); } public static void main(String[] args) { double total = totalWithTax(49.99); System.out.println(total + " " + CURRENCY); // 53.739... USD } }

Example 3: Game Config

public class GameConfig { public static final int MAX_LIVES = 3; public static final int BOARD_WIDTH = 800; public static final int BOARD_HEIGHT = 600; public static final double GRAVITY = 9.81; public static final String TITLE = "Space Run"; public static void main(String[] args) { System.out.println(TITLE); System.out.println(BOARD_WIDTH + "x" + BOARD_HEIGHT); } }

Decision: final or Not?

New variable declared Will the value ever change? YES Regular var NO Used in multiple methods/classes? YES static final (class-level) NO final (local)

Naming Best Practices

1
UPPER_SNAKE_CASE: Separate words with underscores: MAX_SPEED, DEFAULT_COLOR.
2
Be descriptive: INTEREST_RATE over IR. A reader should understand the constant without context.
3
Avoid abbreviations: NUM_PLAYERS is clear; NP is not.
4
Group related constants: Put them in a dedicated class or at the top of the class they belong to.

Common Mistakes

✕ Forgetting final
Declaring double TAX = 0.08; without final. Now it can be reassigned accidentally.
✕ Wrong naming
Using camelCase for constants: final int maxSize = 10; violates convention and confuses readers.
✕ Trying to reassign
Writing MAX = 20; later in the code. This triggers a compile-time error.

Question 1 of 5

Which keyword makes a variable's value unchangeable in Java?

Question 2 of 5

What is the correct naming convention for a constant in Java?

Question 3 of 5

What happens if you try to reassign a final variable?

Question 4 of 5

Which declaration creates a class-level constant accessible from anywhere?

Question 5 of 5

What is the result of compiling and running this code?