In this tutorial, we will understand what is Dart final and constant. How to define constants in Dart. The most important question is Why do we need final and const in dart and what is the purpose of these keywords.
Dart Final and Constant
If you never want to change a value then use final and const keywords.
There are so many situations where we don’t want to change the value of a variable. As an example the value of Pi.
const PI=3.14;
Or maybe you don’t want to change a name throughout the whole program.
final name=”Waheed”;
The PI constant will contain the value 3.14 and final name will have the value ‘Waheed’ throughout the code. Now in both cases, you won’t be able to change the value of either PI or name variable. The common question that comes in mind is What is the difference between final and const.
Difference Between Final and Const
- A final variable can only be set once and it is initialized only when it is accessed.
- Here the focus point is “initialized only when it is accessed” mean If you use the final variable in your program only then the value is initialized the and memory location is allocated. And if you never use the final variable the value will never be initialized in your program.
- A const variable is implicitly final but it is a compilation time constant.
- It means all the const variables are also final.
- It is a compiled time constant mean it is initialized during the compilation. Whenever you compile the program the value of PI will be initialized and memory will be allocated. It does not matter if you are using this variable or not.
- An instance variable can be final but cannot be const.
- If you want to make a Constant at class level declare it as static const not only const.
Now it time to clear the concepts with an actual example.
void main() { final name='Waheed'; name='Batman'; }
We have defined a final variable name and then later on we are trying to change the value of final name variable but we will get an error message like this.
‘error
You may have noticed when haven’t defined data type for the variable name. Well, Dart understands it a String by its literal value. You can explicitly define it as a String but it is optional. We have cleared these concepts in String, Literals and String Interpolation tutorial.
Let’s see an example of const as well.
void main() { const PI=3.14; const burjKhalifaHeight=828; }
This is how we define const in Dart.
There is another very important concept to understand. Remember we have studied that ‘An instance variable can be final but cannot be const.’ Let’s clear this concept with the help of an example. For that, we have to created a Class. If you are not familiar with what is class please don’t worry we will cover the classes concept very soon. For
void main() { const PI=3.14; const burjKhalifaHeight=828; } class Animal { final name='Horse'; const weight= 80; }
Well, the compiler will give us an error at const weight. ‘ Only static fields can be declared as const‘.
So if you define a const at a Class level then you have to use the static keyword as well. We will cover what is a static keyword but for now, just remember we cannot simply declare a const variable at Class level. It must be static const.
void main() { const PI=3.14; const burjKhalifaHeight=828; } class Animal { final name='Horse'; static const weight= 80; }
That is a very short but detailed tutorial about dart final and constant. We are sure now you will never forget the final and const concepts. Please enable the notifications so you never miss any update about Dart language.