Skip to content

Kotlin Object

Devrath edited this page Aug 24, 2022 · 1 revision
  • In Java if we want to access the member field and member function of a class we use the static keyword.
  • The static keyword represents the singleton pattern. And by the singleton pattern, we are able to have one instance of a class.
  • We create singleton so that we can share the same instance and resources throughout and sometimes the reason can be because these resources are expensive to create.
  • Can the object class be inherited? --> No, Instead we need to enclose it in an open class and then need to get access to the items of it
  • Can we have a constructor in the object class? --> No, Instead we can have a init{} method and initialize anything if needed
Object Singleton {
      var count : Int = 0;
      fun appendCount(){
            count++
      }
}
Clone this wiki locally