Skip to lesson
MentorNxt home

Java Programming

Variables & Data Types

Constants with final

A constant is a value that never changes while the program runs, like the pass marks of 35. In Java, the keyword final makes one.

What is a constant?

A results program uses the pass marks, 35, in many places; if one line changes it to 40 by mistake, the results go wrong. A constant is a variable whose value never changes. Write final before the type to make one.

Syntax
final type NAME = value;

Example 1 · Exam constants

Exam.javaJava
public class Exam {    public static void main(String[] args) {        final int PASS_MARKS = 35;        final int MAX_MARKS = 100;        final String COLLEGE_NAME = "ABC College";        int myMarks = 72;        System.out.println(COLLEGE_NAME);        System.out.println("Pass marks: " + PASS_MARKS);        System.out.println("Max marks: " + MAX_MARKS);        System.out.println("My marks: " + myMarks);    }}
Output
ABC College
Pass marks: 35
Max marks: 100
My marks: 72

What this code does

  • final means "this is the last value it gets". PASS_MARKS, MAX_MARKS and COLLEGE_NAME are constants, with UPPER_CASE names.
  • myMarks is a normal variable, so it uses camelCase and could change later.

Java stops you from changing it

If any line tries to change a final variable, the program does not compile: PASS_MARKS = 40; gives cannot assign a value to final variable PASS_MARKS. The mistake is caught before the program ever runs.

final works with any type: final double GST_RATE = 18.0; (an example rate) or final String COLLEGE_NAME = "ABC College";. Only make a value final when it really never changes; a running total is a normal variable. Find the wrong line in each program below.

Find the bug

Constant or not?

Select the line that is wrong.

This should print Pass marks: 35.

Round 1 of 3
For a String or any other reference type, final stops the variable from pointing at a different object; the OOP section shows what that means for objects you can change.

Name constants in UPPER_CASE

In a long program you want to see at a glance which names are fixed. Constants use UPPER_CASE: all capitals with _ between words, like MAX_MARKS. Normal variables use camelCase, like myMarks.

Example 2 · Constants in a sum

Fees.javaJava
public class Fees {    public static void main(String[] args) {        final int HOSTEL_FEE = 45000;        final int MESS_FEE = 30000;        int totalFee = HOSTEL_FEE + MESS_FEE;        System.out.println("Total fee: " + totalFee);        totalFee = totalFee + 5000;        System.out.println("With bus fee: " + totalFee);    }}
Output
Total fee: 75000
With bus fee: 80000

What this code does

  • You can read constants and use them in a sum, like any variable.
  • totalFee is not final, so it can change: totalFee = totalFee + 5000; takes the old value, adds 5000 and stores the result.

UPPER_CASE is a Java convention (a habit everyone follows), not a compiler rule. Other constant names in this style: DAYS_IN_WEEK, GST_RATE.

Next step

Check your understanding

6 quick questions