Semantic difference between object expressions and declarations

  • object expressions are executed (and initialized) immediately, where they are used;
  • object declarations are initialized lazily, when accessed for the first time;
  • a companion object is initialized when the corresponding class is loaded (resolved), matching the semantics of a Java static initializer.

https://kotlinlang.org/docs/reference/object-declarations.html#companion-objects

posted @ 2018-01-11 13:38  YorkYu  阅读(171)  评论(0编辑  收藏  举报