What is the difference between const
and final
keyword in Dart?
15 Answers
There is a post on dart's website and it explains it pretty well.
Final:
"final" means single-assignment: a final variable or field must have an initializer. Once assigned a value, a final variable's value cannot be changed. final modifies variables.
Const:
"const" has a meaning that's a bit more complex and subtle in Dart. const modifies values. You can use it when creating collections, like const [1, 2, 3], and when constructing objects (instead of new) like const Point(2, 3). Here, const means that the object's entire deep state can be determined entirely at compile time and that the object will be frozen and completely immutable.
Const objects have a couple of interesting properties and restrictions:
They must be created from data that can be calculated at compile time. A const object does not have access to anything you would need to calculate at runtime. 1 + 2 is a valid const expression, but new DateTime.now() is not.
They are deeply, transitively immutable. If you have a final field containing a collection, that collection can still be mutable. If you have a const collection, everything in it must also be const, recursively.
They are canonicalized. This is sort of like string interning: for any given const value, a single const object will be created and re-used no matter how many times the const expression(s) are evaluated.
So, what does this mean?
Const:
If the value you have is computed at runtime (new DateTime.now()
, for example), you can not use a const for it. However, if the value is known at compile time (const a = 1;
), then you should use const
over final
. There are 2 other large differences between const
and final
. Firstly, if you're using const
, you have to declare it as static const
rather than just const
. Secondly, if you have a const
collection, everything inside of that is in const
. If you have a final
collection, everything inside of that is not final
.
Final:final
should be used over const
if you don't know the value at compile time, and it will be calculated/grabbed at runtime. If you want an HTTP response that can't be changed, if you want to get something from a database, or if you want to read from a local file, use final
. Anything that isn't known at compile time should be final
over const
.
With all of that being said, both const
and final
cannot be reassigned, but fields in a final
object, as long as they aren't const
or final
themselves, can be reassigned (unlike const
).
Consolidated @Meyi and @faisal-naseer answers and Comparing with little programming.
const:
const keyword used to make a variable to store a compile time constant value. Compile time constant value is a value which will be constant while compiling :-)
For example 5
is a compile time constant. While DateTime.now()
which is not compile time constant. Because this method will return the time when the line is getting executed at runtime. So we can't assign the DateTime.now()
to a const
variable.
const a = 5;
// Uncommenting below statement will cause compile time error.
// Because we can't able to assign a runtime value to a const variable
// const b = DateTime.now();
Should be initialized at the same line.
const a = 5;
// Uncommenting below 2 statement will cause compilation error.
// Because const variable must be initialized at the same line.
// const b;
// b = 6;
All statements mentioned below are acceptable.
// Without type or var
const a = 5;
// With a type
const int b = 5;
// With var
const var c = 6;
Class level const variable should be initialized like below.
Class A {
static const a = 5;
}
Instance level const variable is not possible.
Class A {
// Uncommenting below statement will give compilation error.
// Because const is not possible to be used with instance level
// variable.
// const a = 5;
}
The another major use of const
is used to make the object immutable. To make a class object immutable we need to use the const keyword with constructor and make all the fields as final like mentioned below.
Class A {
final a, b;
const A(this.a, this.b);
}
void main () {
// There is no way to change a field of object once it's
// initialized.
const immutableObja = const A(5, 6);
// Uncommenting below statement will give compilation error.
// Because you are trying to reinitialize a const variable
// with other value
// immutableObja = const A(7, 9);
// But the below one is not the same. Because we are mentioning objA
// is a variable of a class A. Not const. So we can able to assign
// another object of class A to objA.
A objA = const A(8, 9);
// Below statement is acceptable.
objA = const A(10, 11);
}
we can use const keyword to a list.
const a = const [] - A variable a
initialized as const
which contains a list of const
objects(i.e., The list should contain only compile time constant and immutable objects). So we can't able to assign a
with another list.
var a = const [] - A variable a
initialized as var
which contains a list const
objects. So we can able to assign another list to the variable a
.
Class A {
final a, b;
const A(this.a, this.b);
}
class B {
B(){ // Doing something }
}
void main() {
const constantListOfInt = const [5, 6, 7,
// Uncommenting below statement give compilation error.
// Because we are trying to add a runtime value
// to a constant list
// DateTime.now().millisecondsSinceEpoch
];
const constantListOfConstantObjA = const [
A(5, 6),
A(55, 88),
A(100, 9),
];
// Uncommenting below 2 statements will give compilation error.
// Because we are trying to reinitialize with a new list.
// constantListOfInt = [8, 9, 10];
// constantListOfConstantObjA = const[A(55, 77)];
// But the following lines are little different. Because we are just
// trying to assign a list of constant values to a variable. Which
// is acceptable
var variableWithConstantList = const [5, 6, 7];
variableWithConstantList = const [10, 11, 15];
var variableOfConstantListOfObjA = const [A(5, 8), A(7, 9), A(10, 4)];
variableWithConstantList = const [A(9, 10)];
}
final:
final keyword also used to make the variable to hold a constant value. Once initialized we can't able to change the value.
final a = 5;
// Uncommenting below statement will give compilation error.
// Because a is declared as final.
// a = 6;
All statements mentioned below are acceptable.
// Without type or var
final a = 5;
// With a type
final int b = 5;
// With var
final var c = 6;
Can able to assign a runtime value.
// DateTime.now() will return the time when the line is getting
// executed. Which is a runtime value.
final a = DateTime.now();
var b = 5;
final c = b;
Class level final variable must be initialized in the same line.
Class A {
static final a = 5;
static final b = DateTime.now();
}
Instance level final variable must be initialized in the same line or in the constructor initialization. The value will be put into memory when the object is created.
Class A {
final a = 5;
}
// Constructor with a parameter.
Class B {
final b;
B(this.b);
}
// Constructor with multiple parameter.
Class C {
final c;
C(this.c, int d) {
// Do something with d
}
}
void main() {
A objA = new A();
B objB = new B(5);
C objC = new C(5, 6);
}
Assigning a list.
final a = [5, 6, 7, 5.6, A()];
// Uncommenting Below statement will give compilation error.
// Because we are trying to reinitialize the object with another list.
// a = [9.9, 10, B()];
Extending the answer by @Meyi
- final variable can only be set once and it is initialized when
accessed.(for example from code section below if you use the value of
biggestNumberOndice
only then the value will be initialized and memory will be assigned). const is internally final in nature but the main difference is that its compile time constant which is initialized during compilation even if you don't use its value it will get initialized and will take space in memory.
Variable from classes can be final but not constant and if you want a constant at class level make it static const.
Code:
void main() {
// final demonstration
final biggestNumberOndice = '6';
// biggestNumberOndice = '8'; // Throws an error for reinitialization
// const
const smallestNumberOnDice = 1;
}
class TestClass {
final biggestNumberOndice = '6';
//const smallestNumberOnDice = 1; //Throws an error
//Error . only static fields can be declared as constants.
static const smallestNumberOnDice = 1;
}
Both final
and const
prevent a variable from being reassigned (similar to how final
works in Java or how const
works in JavaScript).
The difference has to do with how memory is allocated. Memory is allocated for a final
variable at runtime, and for a const
variable at compile-time. The final
modifier should be the more commonly used, because many program variables won't need any memory since the program logic won't call for them to be initialized. With a const
variable you are basically telling the computer, "Hey, I need memory for this variable up front because I know I'm going to need it."
Thinking of them in this way makes it easier to understand differences in their syntactical usage. Mainly that a final
variable may be an instance variable, but a const
must be a static
variable on a class. This is because instance variables are created at runtime, and const
variables--by definition--are not. Thus, const
variables on a class must be static
, which means simply that a single copy of that variable exists on a class, regardless of whether that class is instantiated.
This video breaks it down fairly simply: https://www.youtube.com/watch?v=9ZZL3iyf4Vk
This article goes into more depth and explains a very important semantic difference between the two, i.e. final
modifies variables and const
modifies values, which essentially boils down to only being able to initialize const
values which are derivable at compile-time.
https://news.dartlang.org/2012/06/const-static-final-oh-my.html
my understand
const
means its initial value is must be fixed, can not be a dynamic value;
final
means its initial value is must be fixed but can be a dynamic value, equal to the var
with a fixed value.
code demos
const
void main() {
const sum = 1 + 2;
// ✅ const can not change its value
print("sum = ${sum}");
// ⚠️ Const variables must be initialized with a constant value.
const time = new DateTime.now();
// ❌ Error: New expression is not a constant expression.
print("time = ${time}");
}
final
// new DateTime.now();
// dynamic timestamp
void main() {
final sum = 1 + 2;
// ✅ final can not change its value
print("sum = ${sum}");
final time = new DateTime.now();
// ✅ final === var with fixed value
print("time = ${time}");
}
Screenshots
refs
https://dart.dev/guides/language/language-tour#final-and-const
When to use which keyword?
A simple example for both: Use final: If you don’t know what it’s value will be at compile-time. For example, when you can need to get data from an API, this happens when running your code.
Use const: If you are sure that a value isn’t going to be changed when running your code. For example, when you declare a sentence that always remains the same.
https://itnext.io/difference-between-const-and-final-in-dart-78c129d0c573
If you never intend to change a variable, use final
or const
, either instead of var or in addition to a type. A final variable can be set only once; a const variable is a compile-time constant. (Const variables are implicitly final.) A final top-level or class variable is initialized the first time it’s used.
Here’s an example of creating and setting a final
variable:
final name = 'Bob'; // Without a type annotation
final String nickname = 'Bobby';
You can’t change the value of a final
variable:
name = 'Alice'; // Error: a final variable can only be set once.
Use const
for variables that you want to be compile-time constants. If the const
variable is at the class level, mark it static
const. Where you declare the variable, set the value to a compile-time constant such as a number or string literal, a const variable, or the result of an arithmetic operation on constant numbers:
const bar = 1000000; // Unit of pressure (dynes/cm2)
const double atm = 1.01325 * bar; // Standard atmosphere
The const
keyword isn’t just for declaring constant variables. You can also use it to create constant values, as well as to declare constructors that create constant values. Any variable can have a constant value.
var foo = const [];
final bar = const [];
const baz = []; // Equivalent to `const []`
You can omit const
from the initializing expression of a const
declaration, like for baz
above. For details, see DON’T use const redundantly.
You can change the value of a non-final, non-const variable, even if it used to have a const
value:
foo = [1, 2, 3]; // Was const []
You can’t change the value of a const
variable:
baz = [42]; // Error: Constant variables can't be assigned a value.
You can define constants that use type checks and casts (is
and as
), collection if, and spread operators (... and ...?
):
const Object i = 3; // Where i is a const Object with an int value...
const list = [i as int]; // Use a typecast.
const map = {if (i is int) i: "int"}; // Use is and collection if.
const set = {if (list is List<int>) ...list}; // ...and a spread.
For more information on using const to create constant values, see Lists, Maps, and Classes.
All these answers I can describe in a concise way.
Final variable can be declared without initialization. But after initializing or assigning a value we can't re-assign it.
Const variable has to initialize with a value while declaring it and obviously similar to Final, we can't re-assign it.
Look at the screenshot.
N.B. The RED mark defines the error and the BLUE one defines - declared but not used.
A variable with the final
keyword will be initialized at runtime and can only be assigned for a single time.
A variable with the const
keyword is initialized at compile-time and is already assigned when at runtime.
Use final
: If you don’t know what it’s value will be at compile-time. For example, when you can need to get data from an API, this happens when running your code.
Use const
: If you are sure that a value isn’t going to be changed when running your code. For example, when you declare a sentence that always remains the same.
const
: stackoverflow.com/questions/51576209/… and the simple explonation tofinal
is constant(cannot reassign or assign once created with final keyword) and you have to initialize it once. – Blasanka