Final and lazy top-level initialization

Please help me understand what exactly this means:

Quote from Chapter 2. A Review of the Language of Gifting

A local, top, or class variable declared as final is initialized on first use

So this is my test code:

lazyTest(msg) => print(msg);

class Printer{
  Printer(msg){
    print(msg);
  }
  final finalClassVariable = lazyTest("final class variable");
}

var globalsAreLazy = lazyTest("top-level");
var lazyInitialized = lazyTest("lazy initialized");

void main() {

   final localFinal = new Printer("local final");
   var initialize = lazyInitialized;
}

Conclusion:

final class variable
local final
lazy initialized

Both finalClassVariableand localFinalinitialized and just globalsAreLazyhave not been. lazyInitializedwas initialized on access as I expected.

+4
source share
2 answers

Class variables are a different name for static fields, so you need to make finalClassVariablestatic lazy for it.

. , , , .

. .

+5

finalClassVariable - , . , static.

+4

Source: https://habr.com/ru/post/1539524/


All Articles