What is a difference between a class and object in Kotlin?

Technology CommunityCategory: KotlinWhat is a difference between a class and object in Kotlin?
VietMX Staff asked 3 years ago
  • An object is a singleton. You do not need to create an instance to use it.
  • class needs to be instantiated to be used.

The primary use case of object in Kotlin is because Kotlin tries to do away with static, and primitives, leaving us with a purely object oriented language. Kotlin still uses static and primitives underneath the hood, but it discourages devs to use those concepts any more. Instead, now Kotlin replaces static with singleton object instances. Where you would previously use static field in Java, in Kotlin you will now create an object, and put that field in the object.