Difference between static, final and const in Flutter
Over a period of time, many new programmers and learners come to point of the confusion about some highly important terms and terminology in the flutter. Flutter uses a Dart Programming Language which is statically typed, unlike any other programming language. Dart has its own way of naming and functionality convention. Let’s look at these keywords and make them easily understandable.
- Static Keyword:
‘static’ keyword in a flutter or dart defines the class level variable.
With the use of static, any variable you declare inside of the class becomes the same for every instance of the class. Such data members can be accessed directly without creating any object.
2. Final Keyword:
The ‘final’ keyword in dart is all about the single assignment of the variable.
Once assigned variables cannot be re-assigned i.e. they are completely immutable but it does not necessarily mean we cannot change the internal state of the variable. A final variable must be initialized.
3. Const Keyword:
‘const’ keyword property is pretty much similar to the final keyword.
You might easily understand this through the following line.
All const variables are the final variables.
With the use of the ‘const’ keyword, the objects state is determined entirely at compile-time setting the state to be completely immutable or unchangeable. This modifies the values. The compiler knows in advance about the variable assigned using the const keyword which helps to ease the load for the compiler for creating a new variable every time it runs.
I am glad that you read it down below. I hope this has helped in more or less way, understanding these three keywords properties and what they really mean. Any feedback and reviews are welcomed.
Have a great day!!!
Thank you :)