Reputation: 15945
Check these two examples:
static const
inside of class:
class SessionStorage {
static const String _keySessionExist = 'storage.key';
}
Just a const
outside of the class:
const String _keySessionExist = 'storage.key';
class SessionStorage {
}
static const
variable inside of a class or just having it declared as const outside of it in Dart
?Upvotes: 12
Views: 10826
Reputation: 1663
If you have more than one Class instance with static field at the same time, the value of that field will be stored in memory only once. It can be helpful if you cant define static const
but can define static final
field.
void main() {
final a1 = A();
final a2 = A();
print(a1.b == a2.b); //true
}
class A {
static final _b = B();
final b = _b;
}
class B {}
But be careful with that because Garbage collector don't collect it and memory will be used till app terminated. Moreover unlike for const is that how much memory it takes will be calculated in runtime.
Upvotes: 1
Reputation:
The declaration for cons must be using const
. You have to declare it as static const
rather than just const
.
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.
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. In other words:
getConst() => const [1, 2];
main() {
var a = getConst();
var b = getConst();
print(a === b); // true
}
I think Dart does a pretty good job of keeping the semantics and the keywords nicely clear and distinct. (There was a time where const was used both for const and final. It was confusing.) The only downside is that when you want to indicate a member that is single-assignment and on the class itself, you have to use both keywords: static final.
I suggest you to have a look at this question
What is the difference between the "const" and "final" keywords in Dart?
Upvotes: 24
Reputation: 90175
- Is there any difference or implications between having a
static const
variable inside of a class or just having it declared asconst
outside of it in Dart?
The obvious difference is that the static
version must be referenced with the class name. Other than the change in name resolution, the should be the same.
- Maybe the compiled code changes?
- Which one is more performant?
They're both compile-time constants. There shouldn't be any difference.
- Which one should we follow if the variable is private to the file?
If you want something that's private to a Dart library (which usually means the file), then prefix it with _
. It doesn't matter whether it's global or static
.
Upvotes: 1