Difference Between Constants and Final Variables in Java



In Java, both constants and final variables are used to define variables that cannot be changed after initialization. But they have some differences. In this article, we will learn how they are different.

Final Variable

A final variable in Java means you cannot reassign it after it has been initialized. Whether it is a local variable, instance variable, or static variable, once it's assigned, the value cannot be changed. If you try to do so, a compile-time error will be generated.

public class FinalExample {
   public static void main(String args[]) {
      final int num = 200;
      num = 2544;
   }
}

Let us compile and run the above program, this will give the following result -

FinalExample.java:4: error: cannot assign a value to a final variable num
 num = 2544;
   ^
1 error

Constant in Java

A constant variable is one whose value is fixed and only one copy of it exists in the program. Once you declare a constant variable and assign a value to it, you cannot change its value again throughout the program.

Unlike in the C language, constants are not supported in Java(directly). But, you can still create a constant by declaring a variable static and final. We can declare a constant only at the class level (not inside methods).

  • Once you declare a variable static, it will be loaded into memory at compile time, i.e., only one copy of it is available.

  • Once you declare a variable static and final, you cannot modify its value again.

Example

Following is an example of a constant in Java -

class Data {
   static final int integerConstant = 20;
}
public class ConstantsExample {
   public static void main(String args[]) {
      System.out.println("value of integerConstant: "+Data.integerConstant);
   }
}

Let us compile and run the above program, this will give the following result -

value of integerConstant: 20

Final variable Vs Constant

The main difference between a final variable and a constant (static and final) is that if you create a final variable without the static keyword. Though its value is un-modifiable, a separate copy of the variable is created each time you create a new object.

Where a constant is un-modifiable and has only one copy throughout the program. 

Example

For example, consider the following Java program -

class Data {
   final int integerConstant = 20;
}
public class ConstantExample {
   public static void main(String args[]) {
      Data obj1 = new Data();
      System.out.println("value of integerConstant: "+obj1.integerConstant);
      Data obj2 = new Data();
      System.out.println("value of integerConstant: "+obj2.integerConstant);
   }
}

Let us compile and run the above program, this will give the following result ?

value of integerConstant: 20
value of integerConstant: 20

Here we have created a final variable and are trying to print its value using two objects, though the value of the variable is the same at both instances, since we have used a different object for each, they are copies of the actual variable.

According to the definition of the constant, you need to have a single copy of the variable throughout the program (class). Therefore, to create a constant per the definition, you need to declare it static and final.

Updated on: 2025-04-24T11:00:25+05:30

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements