Parth Patel
Parth Patel

Reputation: 11

What is the meaning of static const and const in dart programming language?

I watch online tutorial on YouTube in which the tutor says instance variable can be final but cannot be const therefor if you want a Constant at class level then make it static const . My question is what is the need to use static const while we have const keyword whose job is to define a variable that is constant throughout the variable.

Video link : https://www.youtube.com/watch?v=IYZqVOH6oSU at 2:24

Upvotes: 1

Views: 1154

Answers (3)

julemand101
julemand101

Reputation: 31209

My question is what is the need to use static const while we have const keyword whose job is to define a variable that is constant throughout the variable.

I think the main reason why you need to add the keyword static when declaring a const field in a class is readability so you are absolutely sure your understand that this variable will be same in every instance of the class.

Confusion can happen since final variables are allowed (and often are) to have different values in each instance of the class since final just means "Once assigned a value, a final variable's value cannot be changed.".

Since const means compile-time constant, it will always stay the same for all instances of a class which means it will always be static. But to make that fully clear for the developer, we need to write static even if the compiler could figure that out itself.

But again, that is only my guess. :)

Upvotes: 1

jamesdlin
jamesdlin

Reputation: 89926

Suppose const on a member did not require static, and you had:

class Foo {
  const k = 42;
}

How do you access it?

Foo.k should be legal; since the value is always the same, you shouldn't need to construct a Foo object first.

What about Foo().k? Since there's no static keyword, it looks like it might be legal too. But if that's allowed, then there are bunch of cases you need to consider and define behaviors for:

Foo fooInstance = null;
print(fooInstance.k);
print(fooInstance?.k);

class Derived extends Foo {
  const k = 9;
}

fooInstance = Derived();
print(fooInstance.k);

It seems simpler and clearer to me to require that const members also be static.

Upvotes: 0

MickaelHrndz
MickaelHrndz

Reputation: 3832

According to this article :

"static", "final", and "const" mean entirely distinct things in Dart:

"static" means a member is available on the class itself instead of on instances of the class. That's all it means, and it isn't used for anything else. static modifies members.

"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" 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.

Upvotes: 1

Related Questions