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.

Answer from Luigi Cortese on Stack Overflow
🌐
TutorialsPoint
tutorialspoint.com › difference-between-constants-and-final-variables-in-java
Difference between constants and final variables in Java?
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 ...
🌐
Reddit
reddit.com › r/programmerhumor › what exactly is the difference between const and final?
r/ProgrammerHumor on Reddit: What exactly is the difference between const and final?
December 31, 2022 - You can. The difference would be that you'd only be able to call const functions on the const member, whereas in Java there's no difference between a final member and a non-final member past the point of initialization, AFAIK.
🌐
Quora
quora.com › What-is-the-difference-between-final-and-const-values-in-Java
What is the difference between final and const values in Java? - Quora
My IDEs tell me it is syntax error. Final does only prevent change of the value not the object properties of the referred Object. This is due the fact objects are stored as reference, and final prevents changing of this reference.
🌐
W3Schools
w3schools.com › java › java_variables_final.asp
Java Constants (final keyword)
Java Examples Java Compiler Java ... value to change, use the final keyword. A variable declared with final becomes a constant, which means unchangeable and read-only:...
🌐
Coderanch
coderanch.com › t › 404016 › java › Difference-const-final-java
Difference between const & final in java ? (Beginning Java forum at Coderanch)
What is difference between const & final in java. Both const & final are used to represent Constant value .
🌐
TheServerSide
theserverside.com › video › Why-we-use-static-final-in-Java-for-constants
Why we use static final in Java for constants | TheServerSide
The reason why global constants in Java use the static and final keywords is because final ensures a variable cannot change, while static ensures only one copy of the constant variable is placed in memory, regardless of how many class instances are created.
🌐
Quora
quora.com › What-is-the-difference-between-final-variable-and-constant-in-Java
What is the difference between final variable and constant in Java? - Quora
Answer (1 of 10): Every constant variable is a final variable but vice versa is not true. Constant variables are a kind of final variable but their value must be known at compile time.
Find elsewhere
Top answer
1 of 11
211

In C++ marking a member function const means it may be called on const instances. Java does not have an equivalent to this. E.g.:

class Foo {
public:
   void bar();
   void foo() const;
};

void test(const Foo& i) {
   i.foo(); //fine
   i.bar(); //error
}

Values can be assigned, once, later in Java only e.g.:

public class Foo {
   void bar() {
     final int a;
     a = 10;
   }
}

is legal in Java, but not C++ whereas:

public class Foo {
   void bar() {
     final int a;
     a = 10;
     a = 11; // Not legal, even in Java: a has already been assigned a value.
   }
}

In both Java and C++ member variables may be final/const respectively. These need to be given a value by the time an instance of the class is finished being constructed.

In Java they must be set before the constructor has finished, this can be achieved in one of two ways:

public class Foo {
   private final int a;
   private final int b = 11;
   public Foo() {
      a = 10;
   }
}

In C++ you will need to use initialisation lists to give const members a value:

class Foo {
   const int a;
public:
   Foo() : a(10) {
      // Assignment here with = would not be legal
   }
};

In Java final can be used to mark things as non-overridable. C++ (pre-C++11) does not do this. E.g.:

public class Bar {
   public final void foo() {
   }
}

public class Error extends Bar {
   // Error in java, can't override
   public void foo() {
   }
}

But in C++:

class Bar {
public:
   virtual void foo() const {
   }
};

class Error: public Bar {
public:
   // Fine in C++
   virtual void foo() const {
   }
};

this is fine, because the semantics of marking a member function const are different. (You could also overload by only having the const on one of the member functions. (Note also that C++11 allows member functions to be marked final, see the C++11 update section)


C++11 update:

C++11 does in fact allow you to mark both classes and member functions as final, with identical semantics to the same feature in Java, for example in Java:

public class Bar {
   public final void foo() {
   }
}

public class Error extends Bar {
   // Error in java, can't override
   public void foo() {
   }
}

Can now be exactly written in C++11 as:

class Bar {
public:
  virtual void foo() final;
};

class Error : public Bar {
public:
  virtual void foo() final;
};

I had to compile this example with a pre-release of G++ 4.7. Note that this does not replace const in this case, but rather augments it, providing the Java-like behaviour that wasn't seen with the closest equivalent C++ keyword. So if you wanted a member function to be both final and const you would do:

class Bar {
public:
  virtual void foo() const final;
};

(The order of const and final here is required).

Previously there wasn't a direct equivalent of const member functions although making functions non-virtual would be a potential option albeit without causing an error at compile time.

Likewise the Java:

public final class Bar {
}

public class Error extends Bar {
}

becomes in C++11:

class Bar final {
};

class Error : public Bar {
};

(Previously private constructors was probably the closest you could get to this in C++)

Interestingly, in order to maintain backwards compatibility with pre-C++11 code final isn't a keyword in the usual way. (Take the trivial, legal C++98 example struct final; to see why making it a keyword would break code)

2 of 11
38

A const object can only call const methods, and is generally considered immutable.

const Person* person = myself;
person = otherPerson; //Valid... unless we declared it const Person* const!
person->setAge(20); //Invalid, assuming setAge isn't a const method (it shouldn't be)

A final object cannot be set to a new object, but it is not immutable - there is nothing stopping someone from calling any set methods.

final Person person = myself;
person = otherPerson; //Invalid
person.setAge(20); //Valid!

Java has no inherent way of declaring objects immutable; you need to design the class as immutable yourself.

When the variable is a primitive type, final/const work the same.

const int a = 10; //C++
final int a = 10; //Java
a = 11; //Invalid in both languages
🌐
Scaler
scaler.com › topics › constant-in-java
What is Constant in Java? - Scaler Topics
September 9, 2022 - Constants can be declared using Java's static and final keywords. The static keyword is used for memory management, and the final keyword signifies the property that the variable's value cannot be changed. It makes the primitive data types immutable. According to Java Constant in Java is a ...
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Why-the-const-keyword-in-Java-is-not-implemented
Why the Java const keyword is unimplemented
The const keyword is not implemented in Java because Java’s final keyword does a better job of expressing what it means to be a constant in an object-oriented system.
🌐
ThoughtCo
thoughtco.com › constant-2034049
Here's How to Create a Java Constant Variable
May 13, 2025 - Final keyword ensures the variable's reference stays consistent but doesn't prevent changes to mutable objects. A constant is a variable whose value can't change once assigned. Java doesn't have built-in support for constants, but you can use ...
🌐
Wikipedia
en.wikipedia.org › wiki › Final_(Java)
final (Java) - Wikipedia
October 29, 2025 - By contrast, when applying const to the referenced data only, as in const SomeClass* ptr, the contents cannot be modified (without casting), but the reference itself can. Both the reference and the contents being referenced can be declared as const. In C++, the final keyword is used to denote that a function cannot be further overridden. It is also used similarly to Java to declare a class as final (cannot be extended).
🌐
Java Mex
javamex.com › java_equivalents › const_java.shtml
The Java equivalent of 'const': Java code
The Java equivalent of const depends ... follows: First, the combinations that are relatively easy to immitate in Java. The Java final modifier marks a variable as unmodifiable once it has been set....
Top answer
1 of 3
20
Ah, a topic near and dear to my heart. I love C++ const, but have been writing java 24/7 for the last few years. I don't think it is fair to say that const was replaced by final they are superficially similar, and there is some relationship between C++ and Java but I don't think it is too useful to focus on. Technically the differences are, C++ Const The const keyword applies to methods as well as variables. Variables declared const can only invoke const methods. The mutable keyword allows to ignore const correctness. Java final Can refer to classes as well as methods and variables, but isn't particularly relevant for this question I think. Only protects primitives, can still modify referenced objects through final references (ie I can invoked setters). Personally I think the C++ approach is superior, and have yet to see any language replicate it, a lot of languages try to enforce immutability in the standard library data structure hierarchy (ie MutableList vs ImmutableList), which seems like an unfortunate amount of extra work to me. If I could have a single C++ language feature in other languages, const would be it.
2 of 3
1
Just to expound a little on the Java half (I feel like I barely remember c++ after nearly 20 years)... A final class cannot be extended, inherited from to create a new class. A final method cannot be overridden. A final variable has a fixed value, rendering it constant. This plays out in a couple of ways. Primitive variables (int, float, char) store the value of the primitive (1, 3.14, ‘A’), rendering them constant. Class variables, like Integer a = new Integer(1); store an address to that object in memory. The address stored in the variable becomes constant, but you can still mutate the object it references. Strings are immutable in Java. You cannot alter the contents of a String object. Operating on a String object, like s = “foo”; s += “bar”; results in a whole new String object in memory, new String(s + “bar”); so in effect Strings become constant. The most common use (only use really) I’ve had for the final modifier in the last 10 years is to supply external information to internal methods or inline anonymous implementations of an interface, without having an actual callback. And this only perhaps 3 or 4 times across dozens of projects. Here’s some light reading: https://www.geeksforgeeks.org/final-keyword-java/
🌐
YouTube
youtube.com › watch
Why Java Uses static & final for Constants - YouTube
In Java, the `static final` combination is used to create constants, rather than using `const` as in some other programming languages. This choice is due to ...
Published   February 3, 2024
🌐
GeeksforGeeks
geeksforgeeks.org › dart › dart-const-and-final-keyword
Dart - Const And Final Keyword - GeeksforGeeks
April 2, 2025 - The final keyword is used to hardcode the values of the variable, and it cannot be altered in the future; neither can any kind of operations performed on these variables alter its value (state).
🌐
Hyperskill
hyperskill.org › learn › step › 7427
Constants. Final variables
Hyperskill is an educational platform for learning programming and software development through project-based courses, that helps you secure a job in tech. Master Python, Java, Kotlin, and more with real-world coding challenges.