How to declare constants in Java?

In Java, constants are typically declared using the final keyword. The final keyword in Java is used to indicate that the value of a variable cannot be changed once it has been assigned. Constants are often declared with the static keyword as well, especially when they are meant to be shared across all instances of a class. This combination makes the constant a static final variable, which is a common practice for defining constants in Java.

Declaring Constants in Java

Here are the steps to declare constants in Java:

  1. Use the final Keyword: To ensure that the variable's value cannot be modified once it is set.
  2. Use the static Keyword: To make the constant a class-level variable, which means it can be accessed without creating an instance of the class.
  3. Use Uppercase Letters with Underscores: By convention, constant names are written in all uppercase letters with underscores separating words.

Example of Declaring Constants

Here is an example demonstrating how to declare constants in Java:

Example: Simple Constants

public class ConstantsExample { // Constants public static final int MAX_USERS = 100; public static final String APPLICATION_NAME = "MyApplication"; public static final double PI = 3.141592653589793; public static void main(String[] args) { System.out.println("Max Users: " + MAX_USERS); System.out.println("Application Name: " + APPLICATION_NAME); System.out.println("Value of PI: " + PI); } }

In this example:

  • MAX_USERS is an integer constant with a value of 100.
  • APPLICATION_NAME is a string constant with a value of "MyApplication".
  • PI is a double constant representing the value of π.

Example: Constants in an Interface

Another way to declare constants is within an interface. Constants in an interface are implicitly public, static, and final.

public interface Config { int MAX_USERS = 100; String APPLICATION_NAME = "MyApplication"; double PI = 3.141592653589793; } public class ConstantsInterfaceExample { public static void main(String[] args) { System.out.println("Max Users: " + Config.MAX_USERS); System.out.println("Application Name: " + Config.APPLICATION_NAME); System.out.println("Value of PI: " + Config.PI); } }

Best Practices

  • Naming Conventions: Use uppercase letters and underscores to separate words in constant names.
  • Immutable Values: Ensure that the values assigned to constants are immutable. For example, using strings and primitive types.
  • Class for Constants: If you have a large number of constants, consider creating a dedicated class to store them. This class can be a utility class that only contains static final fields.

Example: Utility Class for Constants

public class AppConstants { public static final int MAX_USERS = 100; public static final String APPLICATION_NAME = "MyApplication"; public static final double PI = 3.141592653589793; }

Usage:

public class ConstantsUtilityClassExample { public static void main(String[] args) { System.out.println("Max Users: " + AppConstants.MAX_USERS); System.out.println("Application Name: " + AppConstants.APPLICATION_NAME); System.out.println("Value of PI: " + AppConstants.PI); } }

By following these guidelines, you can effectively declare and manage constants in your Java applications, ensuring that their values remain unchanged and are easily accessible throughout your codebase.

TAGS
System Design Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What rank is Dell?
Which are Splunk license types?
Which is the most toughest interview?
What is Write-Ahead Logging vs Snapshotting in Databases?
Is LLD the same as system design?
What is the best certification for a software engineer?
Related Courses
New
Grokking Agentic Design Patterns course cover
Grokking Agentic Design Patterns
Design agents that act safely, recover from failure, and justify their cost.
4.9
(561 learners)
Discounted price for Your Region

$98

New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
Discounted price for Your Region

$98

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Design Gurus logo
One-Stop Portal For Tech Interviews.
About Us
Our Team
Careers
Partners
Contact Us
Become Affiliate
Copyright © 2026 Design Gurus, LLC. All rights reserved.