How to Declare a Constant in Java with static final
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
static final <type> <NAME> = <value>;
| Part | What it does |
|---|---|
static | Makes the field belong to the class, so there's one copy and no object is needed to use it. |
final | Stops 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
.javafile directly. This guide uses OpenJDK 25.0.5.1.
Step 1: Write the Code
Create a file named 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
java Main.java
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:
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);
}
}
java Main.java
Main.java:6: error: cannot assign a value to static final variable ISSUER
ISSUER = "https://elsewhere.example.org";
^
1 error
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
ISSUERand stopped withcannot 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
| Mistake | What happens | Do this instead |
|---|---|---|
Leaving out static | Every object gets its own copy of the value | Write static final for class-wide constants |
Leaving out final | Any code can change the "constant" | Always write final |
Making a list final and expecting it to be fixed | The variable can't be reassigned, but the list's contents can still change | Use an unmodifiable list, such as one from List.of(...) |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
error: cannot assign a value to static final variable ISSUER | Code assigns to a constant | Use another variable for the changing value |
error: variable ISSUER might not have been initialized | The static final field was declared without a value | Give 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
- Understanding Class Members, in The Java Tutorials.
- final Fields, in the Java Language Specification.
