1. What is
final?
A final variable can only be assigned once, at declaration. After that, the value is locked.
Regular Variable
25
Click to reassign
Final Variable
120
Try clicking to reassign
int age = 25;
age = 30; // OK — regular variables can be reassigned
final int MAX_AGE = 120;
MAX_AGE = 130; // ERROR: cannot assign a value to final variable MAX_AGE
age = 30; // OK — regular variables can be reassigned
final int MAX_AGE = 120;
MAX_AGE = 130; // ERROR: cannot assign a value to final variable MAX_AGE
2. Why Use Constants?
// Calculating totals for an online store
double subtotal1 = price1 * 0.08; // tax
double subtotal2 = price2 * 0.08; // tax
double subtotal3 = price3 * 0.08; // tax
System.out.println("Tax applied at " + 0.08);
double subtotal1 = price1 * 0.08; // tax
double subtotal2 = price2 * 0.08; // tax
double subtotal3 = price3 * 0.08; // tax
System.out.println("Tax applied at " + 0.08);
Scenario: The tax rate changes to 10%. How many places do you need to update?
Changes needed: 4 places — easy to miss one!
final double TAX_RATE = 0.08;
// Calculating totals for an online store
double subtotal1 = price1 * TAX_RATE;
double subtotal2 = price2 * TAX_RATE;
double subtotal3 = price3 * TAX_RATE;
System.out.println("Tax applied at " + TAX_RATE);
// Calculating totals for an online store
double subtotal1 = price1 * TAX_RATE;
double subtotal2 = price2 * TAX_RATE;
double subtotal3 = price3 * TAX_RATE;
System.out.println("Tax applied at " + TAX_RATE);
Same scenario: The tax rate changes to 10%. How many places do you need to update?
Changes needed: 1 place — just update the constant!
3. Naming Convention
Constants use UPPER_SNAKE_CASE — all uppercase letters with underscores between words. This makes them instantly recognizable in your code.
Correct
final double TAX_RATE = 0.08;final int MAX_STUDENTS = 30;
final String SCHOOL_NAME = "YearUp";
Incorrect (compiles, but wrong style)
final double taxRate = 0.08;final int maxstudents = 30;
final String school_name = "YearUp";
Quick Quiz: Pick the Correct Constant Name
4. Real-World Examples (Rental Car Calculator)
In the Rental Car Calculator exercise, you will use constants like these to store fixed rates.
Declaring them as final makes the code self-documenting — you can read what each number means.
final double CAR_RENTAL_RATE = 29.99;
Daily base rate for renting a car
final double TOLL_TAG_RATE = 3.95;
Optional daily toll tag add-on
final double UNDERAGE_DRIVER_SURCHARGE = 0.30;
30% surcharge for drivers under 25
// Without constants (confusing):
double total = days * 29.99 + days * 3.95;
// With constants (clear and maintainable):
double total = days * CAR_RENTAL_RATE + days * TOLL_TAG_RATE;
double total = days * 29.99 + days * 3.95;
// With constants (clear and maintainable):
double total = days * CAR_RENTAL_RATE + days * TOLL_TAG_RATE;
Tip: Constants make your code easier to read and maintain. When you see a number like
0.08 in your code, ask yourself: should this be a constant?
Click the variable boxes in Section 1 to see the difference between regular and final variables