Kotlin: Difference between object and companion object in a class

后端 未结 6 1232
傲寒
傲寒 2020-12-24 10:12

What is the difference between an object and a companion object in a class in kotlin?

Example:

class MyClass {

    object Holder {
        //somethi         


        
6条回答
  •  感情败类
    2020-12-24 10:37

    An object, or an object declaration, is initialized lazily, when accessed for the first time.

    A companion object is initialized when the corresponding class is loaded. It brings about the 'static' essence, although Kotlin does not inherently support static members.

提交回复
热议问题