0
const String name = "John";
static String name = "Tom";
static const name = "Alex";

I read somewhere that const variables are static by default. Are the three above the same?

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Does this answer your question? [What is the difference between the "const" and "final" keywords in Dart?](https://stackoverflow.com/questions/50431055/what-is-the-difference-between-the-const-and-final-keywords-in-dart) – flakerimi Oct 20 '21 at 17:55
  • The static keyword is used for memory management of global data members. The static keyword can be applied to the fields and methods of a class. The static variables and methods are part of the class instead of a specific instance. https://www.geeksforgeeks.org/dart-static-keyword/ – flakerimi Oct 20 '21 at 17:57
  • They are not the same. [`const` class members are required to be additionally declared `static`](https://stackoverflow.com/questions/17074897/); it's intentionally not automatic. Assuming that `name` is declared within a `class`: the `John` version is illegal, the `Tom` version can be reassigned (and if it were of a mutable type, could be modified). – jamesdlin Oct 20 '21 at 18:30

0 Answers0