31

Please help me understand the difference between constant variables and final variables in Java. I am a bit confused with it.

3
  • 12
    what do you mean by constant variables? How can a constant be variable?
    – Blip
    May 30, 2015 at 11:36
  • 2
    This is not a duplicate of the marked question..
    – user9514304
    Apr 20, 2018 at 1:09
  • 2
    @Ichiro I second you. I voted to reopen it. OP has not asked anything about static at all. He is confused if Java has anything like const and if they are related to final in any way.
    – RBT
    Aug 30, 2018 at 7:21

4 Answers 4

43

Constant is the concept, the property of the variable.

final is the java keyword to declare a constant variable.


As other people pointed out, from a semantic/linguistic point of view the expression constant variable is an oxymoron and, as such, we could argue about its correctness.

Quoting the specification, anyway, we can read

A variable of primitive type [...], that is final and initialized with a compile-time constant expression (§15.28), is called a constant variable.

I suppose, hence, that we can accept (and consider correct) this binomial for our purpose.

1
  • 3
    Could you say the value is variable at compile time yet constant at runtime?
    – jchook
    May 9, 2017 at 1:43
18

Constant is not a keyword in Java.

It is a concept to make any variable constant. For this we use final keyword in Java so that after initializing the variable with final keyword , no one can reassign the value of that variable.

4
  • 3
    What do you mean by "no one can modify the value of that variable."? How about final List<String> list = new ArrayList<>(); list.add("foo");? I am modifying ArrayList (which is held by final list) by adding new elements to it, so people can think that I modifying value of list.
    – Pshemo
    May 30, 2015 at 12:04
  • 1
    @Pshemo You are adding a new object to that list. You are not assigning it again like this - List<String> list1 = new ArrayList<>(); list = list1;
    – Amit Das
    May 30, 2015 at 12:12
  • 8
    Yes, but can't changing state of object held by list can be counted as modifying? I know the difference here, but when you are posting an answer which was meant to clarify one confusion you should not do it by introducing new confusion. So instead "no one can modify the value of that variable" it is better to say no one can reassign new value to final variable.
    – Pshemo
    May 30, 2015 at 12:18
  • finer differences, makes the point whole lot clear. Thanks @Pshemo for persisting ad thanks Amit Das for answer as well as admitting that there is a point here. Aug 16, 2019 at 6:59
5

There are several values in the real world which will never change. A square will always have four sides, PI to three decimal places will always be 3.142, and a day will always have 24 hours. These values remain constant. When writing a program it makes sense to represent them in the same way - as values that will not be modified once they have been assigned to a variable. These variables are known as constants.

Declaring a Variable as a Constant

In declaring variables I showed that it’s easy to assign a value to a int variable:

int hoursInADay = 24;

We know this value is never going to change in the real world so we make sure it doesn’t in the program. This is done by adding the keyword modifier final:

final int HOURS_IN_A_DAY = 24;

In addition to the final keyword you should have noticed that the case of the variable name has changed to be uppercase as per the standard Java naming convention. This makes it far easier to spot which variables are constants in your code.

If we now try and change the value of HOURS_IN_A_DAY:

final int HOURS_IN_A_DAY = 24; 
HOURS_IN_A_DAY = 36;

we will get the following error from the compiler:

cannot assign a value to final variable HOURS_IN_A_DAY

The same goes for any of the other primitive data type variables. To make them into constants just add the final keyword to their declaration.

Where to Declare Constants

As with normal variables you want to limit the scope of constants to where they are used. If the value of the constant is only needed in a method then declare it there:

public class Hours {
   public static final int HOURS_IN_A_DAY = 24;
}
1
  • 4
    Java conventions are that all-uppercase names are reserved for static final variables. May 30, 2015 at 11:57
5

Make variable "final" means we cannot reassign a value to this variable (ie we can use variable = Something once and only once.

So for primitive we can say final variable are constante.

But final variable can be non constant. for exemple

final Stringbuffer string = new StringBuffer("not"); 
string.append(" constant");
System.out.println(string); 

will print the string "not constant". Here "string" variable is final but not constant. i hope this will help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.