Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

How to declare constants in Java

Tarun Telang

Introduction

In Java, a constant is a variable whose value cannot be changed once it's assigned.

Constants are often used to represent values that will not change, such as the number of students in a class. They can also be used to make the code more readable by giving names to values that would otherwise be hard to remember.

A constant can be declared within a method, class, or interface. When declaring constants, it's good practice to use all uppercase letters for the constant name. This makes it easier to identify constants in code.

Syntax

To declare a constant in a class, we use the final keyword:

public final int NUM_STUDENTS = 100;

To declare a constant in an interface, use the static and final keywords:

public static final int NUM_STUDENTS = 100;

Note: The statements above declare a constant named NUM_STUDENTS with a value of 100.

When should we use constants?

There are several cases where using constants can be helpful. They are as follows:

  1. To make code more readable by giving names to values
  2. To prevent a variable from being changed accidentally
  3. To enforce the immutability of an object

Example 1

Here is a Java program demonstrating the use of constant within a method:

public class Main {

    public static void main(String[] args) {
        final int NUM_STUDENTS = 100;
        System.out.println("There are " + NUM_STUDENTS + " students in this class.");
    }
}
Code

Explanation

  • Line 4: We declare a constant named NUM_STUDENTS using the final keyword inside the main method.
  • Line 5: The println command displays the value of the constant at the output.

Example 2

Here is a Java program that demonstrates the use of a constant within a class:

public class Main {
    public static final int NUM_STUDENTS = 100;
    public static void main(String[] args) {
        System.out.println("There are " + NUM_STUDENTS + " students in this class.");
    }
}
Code

Explanation

  • Line 2: We declare a constant named as NUM_STUDENTS using the final keyword inside the Main class. We also use the static keyword that makes the constant available for all instances of the class.
  • Line 4: The println command is displays the value of the constant at the output.

Example 3

Here is a Java program that demonstrates the use of a constant within an interface:

interface IClassConstants {
    int NUM_STUDENTS = 100;
}

public class Main implements IClassConstants{
    public static void main(String[] args) {
        System.out.println("There are " + NUM_STUDENTS + " students in this class.");
    }
}
Code

Explanation

  • Line 2: We declare a constant named NUM_STUDENTS inside the IClassConstants interface. In the case of an interface, public and static keywords are redundant but it is good programming practice to use them.
  • Line 7: The println command displays the value of the constant at the output.

RELATED TAGS

java
RELATED COURSES

View all Courses

Keep Exploring