Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Why do I have to declare "static" in the class?</p> </blockquote> <p>Because instance variables/methods can't be <code>const</code>. This would mean their value could be different per instance, which can't be the case for compile-time constants. <a href="https://www.dartlang.org/docs/dart-up-and-running/contents/ch02.html#ch02-final-const" rel="nofollow">(Source)</a></p> <blockquote> <p>Why can't I declare a "top level" const as "static"?</p> </blockquote> <p>The <code>static</code> modifier marks variables/methods as class-wide (same value for every instance of the class). Top-level stuff is application-wide and doesn't belong to any class, so marking them as class-wide doesn't make any sense and is not allowed. <a href="https://www.dartlang.org/docs/dart-up-and-running/contents/ch02.html#ch02-class-vars-and-methods" rel="nofollow">(Source)</a></p> <blockquote> <p>What is the best or only way to declare compile-time values that cannot be altered?</p> </blockquote> <p>You are already doing it - add <code>static</code> when defining class constants. Don't add it when defining top-level constants. Also, use <code>const</code>. <code>final</code> vars aren't compile-time values.</p> <blockquote> <p>What is the difference between [source code with different constant definitions omitted]</p> </blockquote> <p><code>static const</code> and <code>const</code> is the pretty much the same, usage depends on context. The difference between <code>const</code> and <code>final</code> is that <code>const</code> are compile-time constants - they can only be initialized using literal values (or expressions constisting of operators and literal values) and can't be changed. <code>final</code> variables also can't be changed after being initialized, but they are basically normal variables. This means any kind of expression can be used, and the value can be a different one for every class instance:</p> <pre class="lang-dart prettyprint-override"><code>import "dart:math"; Random r = new Random(); int getFinalValue() { return new Random().nextInt(100); } class Test { // final variable per instance. final int FOO = getFinalValue(); // final variable per class. "const" wouldn't work here, getFinalValue() is no literal static final int BAR = getFinalValue(); } // final top-level variable final int BAZ = getFinalValue(); // again, this doesn't work, because static top-level elements don't make sense // static final int WAT = getFinalValue(); void main() { Test a = new Test(); print(Test.BAR); print(BAZ); // different from Test.BAR print(a.FOO); a = new Test(); print(Test.BAR); // same as before print(BAZ); // same as before print(a.FOO); // not the same as before, because it is another instance, // initialized with another value // but this would still be a syntax error, because the variable is final. // a.FOO = 42; } </code></pre> <p>I hope this helped, and I didn't descibe it too confusing. :]</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload