Assignment Between const and final Variables
A const variable can be assigned to a final variable, but not the other way around.
Here’s why:
- Const to Final:
- A
constvariable, being a compile-time constant, can be assigned to afinalvariable because its value is known at compile time. - The
finalvariable will then hold the same immutable value as theconstvariable.
- A
- Final to Const:
- A
finalvariable, while immutable, might not be a compile-time constant. Its value could be determined at runtime. - Assigning a
finalvariable to aconstvariable would violate theconstrequirement of being known at compile time.
- A
Example:
const pi = 3.14159;
final double circumference = 2 * pi * 5; // Valid
In this example, pi is a const variable, and its value is known at compile time. It’s assigned to the final variable circumference, which is then also a constant value.
However, the following would be invalid:
final DateTime now = DateTime.now();
const DateTime fixedTime = now; // Invalid, now's value is determined at runtime
Here, now is a final variable, but its value is determined at runtime. Assigning it to a const variable would violate the compile-time constant requirement.