Kotlin 2 · Classes and types

Singletons and companion objects

object declares a singleton, and a companion object holds class-level members.

object Config { const val VERSION = "1.0" }

class User private constructor(val name: String) {
    companion object {
        fun create(name: String) = User(name.trim())
    }
}

Official Kotlin 2 documentation

More classes and types commands

All classes and types commands · Kotlin 2 cheatsheet

Was this page helpful?