Skip to main content

How to Declare a Constant in Java with static final

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A Java constant is a field declared static final, such as static final String ISSUER = "https://login.example.org";. static makes it belong to the class, final stops anyone from changing it, and Java refuses to compile code that tries.

Syntax​

Syntax
static final <type> <NAME> = <value>;
PartWhat it does
staticMakes the field belong to the class, so there's one copy and no object is needed to use it.
finalStops the field from being assigned again.
<type>The type of the value, such as String or int.
<NAME>The constant's name, in capital letters with underscores between words.
<value>The value, set once where it's declared.

The Java Tutorials, Understanding Class Members: "The static modifier, in combination with the final modifier, is also used to define constants. The final modifier indicates that the value of this field cannot change."

The Example​

The example declares two constants and prints them. Then a changed version of the program tries to assign a new value to one of them.

Prerequisites​

  • A JDK 11 or later, which can run a single .java file directly. This guide uses OpenJDK 25.0.5.1.

Step 1: Write the Code​

Create a file named Main.java:

Main.java, a class Main with static final String ISSUER and static final int MAX_RETRIES, whose main method prints ISSUER and MAX_RETRIES times 2.

Main.java
public class Main {

static final String ISSUER = "https://login.example.org";
static final int MAX_RETRIES = 3;

public static void main(String[] args) {
System.out.println(ISSUER);
System.out.println(MAX_RETRIES * 2);
}
}

Step 2: Run It​

Terminal
java Main.java
Output
https://login.example.org
6

java Main.java compiles the file in memory and runs it. The command is the same in zsh, bash and PowerShell.

Step 3: Try to Change a Constant​

Replace Main.java with a version whose main method assigns a new value to ISSUER:

Main.java
public class Main {

static final String ISSUER = "https://login.example.org";

public static void main(String[] args) {
ISSUER = "https://elsewhere.example.org";
System.out.println(ISSUER);
}
}
Terminal
java Main.java
Output
Main.java:6: error: cannot assign a value to static final variable ISSUER
ISSUER = "https://elsewhere.example.org";
^
1 error
error: compilation failed

A zsh terminal where java Main.java first prints https://login.example.org and 6, then, for the changed file, fails with error: cannot assign a value to static final variable ISSUER and error: compilation failed.

What Happened​

  • The first version printed both constants, used like any other field.
  • The second version never ran. The compiler found the assignment to ISSUER and stopped with cannot assign a value to static final variable ISSUER, pointing at the line.

The tutorial says so directly: "Constants defined in this way cannot be reassigned, and it is a compile-time error if your program tries to do so." The mistake is caught before the program starts, not while it runs.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out staticEvery object gets its own copy of the valueWrite static final for class-wide constants
Leaving out finalAny code can change the "constant"Always write final
Making a list final and expecting it to be fixedThe variable can't be reassigned, but the list's contents can still changeUse an unmodifiable list, such as one from List.of(...)

Troubleshooting​

SymptomLikely causeFix
error: cannot assign a value to static final variable ISSUERCode assigns to a constantUse another variable for the changing value
error: variable ISSUER might not have been initializedThe static final field was declared without a valueGive the constant its value where it's declared

FAQ​

Why capital letters?​

It's the convention the tutorial describes: "By convention, the names of constant values are spelled in uppercase letters. If the name is composed of more than one word, the words are separated by an underscore (_)."

Is a static final field faster?​

For a primitive or a String with a value known when the code compiles, the compiler copies the value into every place that uses it. The tutorial calls this a compile-time constant.

Conclusion​

static final with a name in capital letters makes a Java constant: one value for the whole class that the compiler won't let anyone change.

Additional Resources​