Object keyword

Kotlin has an object keyword, which combines both declaring a class and creating an instance of it in one action. It can be used in three different situations and has three different meanings. Let's take a look at all of them:

  • Object declaration: Defines a singleton class.
  • Companion object: Defines a nested class that can hold members related to the outer containing class. These members can't require an instance of the outer class.
  • Object expression: Creates an instance of the object on the fly, the same as Java's anonymous inner classes.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.107.229