CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Constant in Java

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor

Constant in Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 3 min read · 0 Comments
Share: in X
Constant in Java

A constant in Java is a variable that is declared with the final keyword, which makes its value unchangeable after initialization. Constants are typically used for values that are known at compile time and do not change during the execution of the program, such as mathematical values (e.g., π), configuration settings, or fixed business rules.

Table of Contents

  • How to Declare a Constant in Java
  • Key Characteristics of Constants
  • Best Practices for Using Constants in Java
  • Common Pitfalls to Avoid
  • Examples of Constants in Java
  • Conclusion

How to Declare a Constant in Java

To declare a constant in Java, we use the final keyword along with the variable’s data type. By convention, constant names are written in UPPER_SNAKE_CASE to distinguish them from regular variables.

Following is the syntax:

final data_type CONSTANT_NAME = value;

For example:

final double PI = 3.14159;
final int MAX_USERS = 100;
final String APPLICATION_NAME = "MyJavaApp";

In these examples:

  • PI is a constant storing the value of π.
  • MAX_USERS is a constant storing the maximum number of users.
  • APPLICATION_NAME is a constant storing the name of the application.

Key Characteristics of Constants

  1. Immutable Value: Once a constant is assigned a value, it cannot be changed. Attempting to modify a constant will result in a compile-time error.
final int MAX_AGE = 100;
MAX_AGE = 120; // Error: Cannot assign a value to final variable 'MAX_AGE'
  1. Initialization: Constants must be initialized at the time of declaration or within the constructor (for instance constants).
final int MIN_AGE; // Error: Variable 'MIN_AGE' might not have been initialized
  1. Static Constants: If a constant is shared across all instances of a class, it should be declared as static final.
static final String DATABASE_URL = "jdbc:mysql://localhost:3306/mydb";

Best Practices for Using Constants in Java

  1. Use Descriptive Names: Choose meaningful names for constants to make your code self-explanatory. For example, use TAX_RATE instead of tr.
  2. Follow Naming Conventions: Constants should be named in UPPER_SNAKE_CASE (e.g., MAX_VALUE, DEFAULT_TIMEOUT).
  3. Group Related Constants: Use a class or interface to group related constants. This improves organization and readability.
public class AppConstants {
    public static final int MAX_USERS = 100;
    public static final int TIMEOUT = 30;
    public static final String DEFAULT_LANGUAGE = "en";
}
  1. Avoid Magic Numbers: Replace hard-coded values with constants to make your code more maintainable and easier to understand.
// Instead of this:
double area = 3.14159 * radius * radius;

// Use this:
double area = PI * radius * radius;

Common Pitfalls to Avoid

  1. Reassigning Constants: Attempting to change the value of a constant will result in a compile-time error.
final int MAX_VALUE = 100;
MAX_VALUE = 200; // Error: Cannot assign a value to final variable 'MAX_VALUE'
  1. Uninitialized Constants: Constants must be initialized at the time of declaration or within the constructor.
final int MIN_AGE; // Error: Variable 'MIN_AGE' might not have been initialized
  1. Overusing Constants: Avoid creating constants for values that are not truly constant or are only used once in the code.

Examples of Constants in Java

  1. Mathematical Constants:
final double PI = 3.14159;
final double E = 2.71828;
  1. Configuration Constants:
final String DATABASE_URL = "jdbc:mysql://localhost:3306/mydb";
final int TIMEOUT = 30;
  1. Business Rule Constants:
final int MAX_LOGIN_ATTEMPTS = 3;
final double TAX_RATE = 0.15;
  1. Grouping Constants in a Class:
public class AppConstants {
    public static final int MAX_USERS = 100;
    public static final int TIMEOUT = 30;
    public static final String DEFAULT_LANGUAGE = "en";
}

Conclusion

Constants are a powerful feature in Java that help us write cleaner, more maintainable, and error-free code. By using the final keyword and following best practices, we can ensure that critical values in our program remain unchanged and easily accessible. Whether we’re defining mathematical values, configuration settings, or business rules, mastering the use of constants is essential for becoming a proficient Java developer.

Related Posts:

  • final Keyword in Java
  • Default Variable Initialization in Java
  • Variable in Java
  • Compile and Run Java Program
  • Identifiers in Java
  • Control Statements in Java
Tags:javalanguage-fundamentals
Was this article helpful?
← Previous ArticleJava Thread Lifecycle
Next Article →Type Conversion and Casting in Java

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap