// Created on savesnippets.com · https://savesnippets.com/3eclaRTWrI46Yv class User private constructor(val id: Long, val name: String) { companion object { private var nextId = 0L fun create(name: String): User = User(++nextId, name) const val MAX_NAME_LENGTH = 50 // Companion objects can implement interfaces too } } fun main() { val u = User.create("Alice") // calls companion factory println("${u.id} ${u.name}") println(User.MAX_NAME_LENGTH) // 50 // Java callers see this as a static field: // User.Companion.create("Alice") — by default // User.create("Alice") — with @JvmStatic on the function }