Kotlin: Difference between object and companion object in a class

后端 未结 6 1267
傲寒
傲寒 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:40

    Companion object exists because you can call companion objects' functions/properties like it is a java static method/field. And for why your Holder is allowed, well, there is no reason that declaring a nested object is illegal. It may comes in handy sometimes.

提交回复
热议问题