companion object: Static-like Members in Kotlin
Add companion objects to classes to simulate static methods and constants.
companion object: Static-like Members in Kotlin is a free Kotlin Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.
No static, but companion object
Kotlin has no static keyword. Instead, a companion object declared inside a class provides class-level (static-like) members.
Declaring a companion object
Put a companion object inside a class. Its members are callable as ClassName.member.
class MathUtils {
companion object {
fun square(x: Int) = x * x
}
}
fun main() {
println(MathUtils.square(5)) // 25
}Constants in companion object
Constants typically live in the companion object, often with const val for compile-time literals.
class HttpClient {
companion object {
const val DEFAULT_TIMEOUT = 5000
const val USER_AGENT = "MyApp/1.0"
}
}
fun main() {
println(HttpClient.DEFAULT_TIMEOUT)
println(HttpClient.USER_AGENT)
}Named companion object
You can name the companion object. Both ClassName.member and ClassName.CompanionName.member work.
class Database {
companion object Factory {
fun create() = Database()
}
}
fun main() {
val db1 = Database.create() // works
val db2 = Database.Factory.create() // also works
println("$db1 / $db2")
}Factory Method Pattern
Use the companion object to create instances with validation or alternative construction logic.
class User private constructor(val name: String, val email: String) {
companion object {
fun create(name: String, email: String): User {
require(email.contains("@")) { "invalid email" }
return User(name, email)
}
}
}
fun main() {
val u = User.create("Ada", "ada@example.com")
println("${u.name} <${u.email}>")
}Implementing Interfaces
The companion object itself can implement interfaces — useful for combining with classes that need a static-like contract.
interface Loadable<T> { fun load(): T }
class Config(val data: String) {
companion object : Loadable<Config> {
override fun load() = Config("loaded data")
}
}
fun main() {
println(Config.load().data)
}@JvmStatic for Java Interop
Annotate companion object members with @JvmStatic to expose them as true static methods to Java callers.
class Util {
companion object {
@JvmStatic
fun greet(name: String) = "Hello, $name!"
}
}
fun main() {
println(Util.greet("Kotlin"))
// In Java: Util.greet("Java")
}Accessing the Companion Object Itself
ClassName.Companion refers to the companion object directly. Use this when passing it as an argument.
class Counter {
companion object {
fun greet() = "hi from companion"
}
}
fun main() {
val ref = Counter.Companion
println(ref.greet())
}Companion vs Top-Level
For utility functions that don't belong to a class, prefer top-level functions over a class with only a companion object.
// File-level — usually cleaner:
fun greet(name: String) = "Hi, $name!"
// Versus:
class Greeter { companion object { fun greet(n: String) = "Hi, $n!" } }
fun main() {
println(greet("Ada"))
println(Greeter.greet("Ada"))
}One Companion per Class
A class can have at most one companion object. Inner regular objects can hold additional shared state if needed.
class Service {
companion object {
val version = "1.0"
}
object InternalHelper { fun log() = println("internal") }
}
fun main() {
println(Service.version)
Service.InternalHelper.log()
}Companion Extension Functions
You can extend the companion object from outside the class, adding fake-static helpers.
class Lib {
companion object
}
fun Lib.Companion.helper() = "added later"
fun main() {
println(Lib.helper())
}Quick Check
What is the Kotlin equivalent of Java's static members?
Recap
Kotlin has no static. Use a companion object for class-level members — constants, factories, interface implementations. Annotate with @JvmStatic for Java interop, or extend with extension functions for fake-static helpers.
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “companion object: Static-like Members in Kotlin” lesson free?
Yes — any 3 lessons of the Kotlin Academy learning path, including “companion object: Static-like Members in Kotlin”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.
What will I learn in “companion object: Static-like Members in Kotlin”?
Add companion objects to classes to simulate static methods and constants. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “companion object: Static-like Members in Kotlin” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- object Declaration: Kotlin Singletons
- companion object: Static-like Members in Kotlin
- Anonymous Objects and Object Expressions
- Factory Methods with companion object