Uppercase convention
- Java course
- Intermediate
- Lesson 37/116
- 4 min
Learn the uppercase naming convention for constants.
Uppercase naming
Constant names are written in uppercase in Java. Multiple words are separated by underscores: MAX_USERS, DEFAULT_TIMEOUT. This convention is called UPPER_SNAKE_CASE and helps distinguish constants from other variables.
Example: Writing static final int max_users = 10; compiles but breaks convention. The correct form is static final int MAX_USERS = 10;. Uppercase immediately shows that this value is a constant and will not change. Java constants like Integer.MAX_VALUE and Math.PI follow this convention. In a consistent codebase, constant names are uppercase; this improves code readability.
Why uppercase?
The uppercase convention distinguishes constants from variables. When reading code, seeing MAX_USERS immediately tells you it will not change. Seeing maxUsers suggests it might change. This distinction quickly conveys the code's intent.
The uppercase convention is a shared agreement in the Java community. Constants in Java libraries like Math.PI, TimeUnit.SECONDS and BorderLayout.CENTER are all uppercase. When your own constants follow this convention, your code stays consistent with the Java ecosystem. Inconsistent naming makes code harder to read and increases maintenance cost.
Practice Add constants to Limits
Define a class Limits. Add a static final int MAX_SIZE = 5000 constant. Add a static final int MIN_SIZE = 100 constant. Add a static final String LABEL = "Size" constant. In main print all three constants' values on separate lines.
Starter code
// Define the Limits class
// Add three constants (uppercase)
// Print in mainSample solution
class Limits {
static final int MAX_SIZE = 5000;
static final int MIN_SIZE = 100;
static final String LABEL = "Size";
public static void main(String[] args) {
System.out.println(Limits.MAX_SIZE);
System.out.println(Limits.MIN_SIZE);
System.out.println(Limits.LABEL);
}
}Expected output
5000 100 Size
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Match naming conventions
| Term | Meaning |
|---|---|
MAX_USERS |
Constant (user) |
userName |
Variable (user) |
MAX_VALUE |
Constant (Java) |
2 Which name is appropriate for a constant?
- maxUsers
- maxusers
- MAX_USERS Correct answer
- MaxUsers
Explanation: MAX_USERS — constants are uppercase with underscores.
3 Complete the constant name
static final int ____ = 5000;
static final int MAX_SIZE = 5000;
Explanation: Constant names are uppercase.