Java

How to Declare Constants in Java

While finalizing the code in Java, there can be a requirement to allocate final or unmodifiable values. For instance, refraining from rewriting confidential entries or omitting ambiguity in the code. In such case scenarios, declaring and utilizing constants in Java is assistive in managing the memory and simplifying the code at the developer’s end.

This blog will illustrate the approaches to “declaring constants” in different scenarios in Java.

What are Constants?

Constant” corresponds to a value that cannot be changed/altered after it is assigned. They enhance the code readability and restrict a variable from being changed accidentally.

How to Declare Constants in Java?

The “final” keyword is utilized to declare constants in Java, as follows:

public final int roll = 15;

 
Example 1: Demonstration of Declaring Constants in Java

In this example, a constant can be declared and overwritten with another value to analyze its behavior in the main:

final int age = 22;
age = 25;
System.out.println("The age is: "+age);

 
In the above lines of code:

    • Declare a constant with a preceding “final” keyword and initialize it with the stated integer.
    • After that, overwrite the constant with another value and display it.
    • This will result in logging an error since the constant value cannot be reassigned or updated.

Output


In the above output, it can be observed that the error is displayed upon overwriting the value of the constant.

Example 2: Declaring Constant Within the Class in Java

In this particular example, a constant can be declared within the class and invoked in “main”:

public static final int age = 18;
System.out.println("The constant value is: "+age);

 
In the above code block, declare a constant within the class with the help of the “static” and “final” keywords, respectively, and invoke it in “main”.

Output


The above output indicates that the constant defined in the class is invoked in the “main” appropriately.

Example 3: Declaring Constants in an Interface in Java

In this particular example, a constant can be declared in an interface and accessed in the main:

interface constant {
final int age = 18;
}
public class declareconstants2 implements constant {
public static void main(String[] args) {
System.out.println("The constant value is: "+age);
}}

 
In the above code snippet, apply the following steps:

    • Create an interface named “constant”. Within this interface, declare the constant named “age” having the specified integer value.
    • In the class declaration, implement the included interface via the “implements” keyword.
    • Finally, invoke the declared constant in the main.

Output


In this outcome, it can be analyzed that the constant is accessed conveniently by implementing the interface.

Conclusion

The “final” keyword is used to declare a constant in Java. The value of the constant is final and cannot be overwritten. It can be declared in the main, within the class, or in an interface. This blog elaborated on the approaches to declaring constants in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.