Skip to content
Learn Netverks

Lesson

Step 16/36 44% through track

objects-companion

Objects and companion

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Objects and companion: the syntax, patterns, and safety habits you need before advancing in Kotlin.

Teams still ship Objects and companion in Kotlin codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Objects and companion in contexts like: Android apps, Spring services, and shared KMP modules.

Write Kotlin in main.kt with fun main(), click Run on server—the dev runner kotlinc compiles to a JVM jar and java runs it; use println for output (requires JDK + kotlinc; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Kotlin singletons use the object keyword—one instance per class loader. Companion objects replace Java static members on the outer class, living inside the class body.

object and companion object

object AppConfig {
    const val VERSION = "1.0"
}

class Factory {
    companion object {
        fun create() = Factory()
    }
}

From Java, companion members appear as Factory.Companion.create() unless annotated with @JvmStatic.

Compared to Java static

Java uses static on the class; Kotlin prefers companion objects and top-level functions in files for namespaced helpers.

Important interview questions and answers

  1. Q: object vs companion object?
    A: object is a true singleton type; companion object is one instance tied to the outer class, often for factories and constants.
  2. Q: How do Java callers see companion members?
    A: As static-like methods on a generated Companion class, or true statics with @JvmStatic.

Self-check

  1. What keyword declares a singleton?
  2. Where do factory methods often live?

Tip: Use companion object for factories; add @JvmStatic for Java Class.method() calls.

Interview prep

object vs class?

object is a singleton instance; class needs construction.

companion vs Java static?

Companion groups static-like members; @JvmStatic for Java.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • object singleton?
  • Companion factory?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump