Deep Kotlin tutorial covering object, companion object, singleton, factory with examples, diagram, mistakes, and interview answers.
Objects, Companion Objects, and Singletons
What is Objects, Companion Objects, and Singletons?
Objects, Companion Objects, and Singletons is an important Kotlin topic from 1.5 Object-Oriented vs Functional Kotlin. In this lesson we focus only on the concepts that belong together: object, companion object, singleton, factory, static-like access. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.
Why students should learn it
- Understand Objects, Companion Objects, and Singletons in simple language
- Use object in real Kotlin or Android code
- Explain Objects, Companion Objects, and Singletons in interviews with examples and trade-offs
How it works
Objects, Companion Objects, and Singletons: object, companion object, singleton, factory, static-like access works by giving Kotlin extra control over instance creation, visibility, and compiled output. object and companion object model a single shared instance, visibility modifiers decide what other files or modules can see, and features such as inline, reified, and destructuring remove boilerplate while the compiler still generates ordinary, predictable bytecode underneath.
- Identify what problem Objects, Companion Objects, and Singletons: object, companion object, singleton, factory, static-like access solves.
- Write the smallest Kotlin snippet that demonstrates it.
- Add one Android or JVM use case where it matters.
- Compare it with a similar concept so the difference is clear.
- Finish by naming one common mistake and the safer alternative.
Interview tip: keep the answer simple first, then add example, trade-off, and mistake.
Types / Classification
- object: core concept in this lesson
- companion object: core concept in this lesson
- singleton: core concept in this lesson
- factory: core concept in this lesson
- static-like access: core concept in this lesson
- object: a class with exactly one instance
- companion object: members callable through the class name
- internal: visible anywhere in the same module
Important differences
| Concept 1 | Concept 2 | How to remember it |
|---|---|---|
| class | object | class can have many instances; object always has exactly one. |
| private | internal | private is visible only in the same file or class; internal is visible anywhere in the same module. |
| Regular function | inline function | A regular function is a normal call; an inline function's body is inserted at the call site, avoiding lambda allocation. |
Diagram
flowchart TD
A["1.5 Object-Oriented vs Functional Kotlin"] --> B["Objects, Companion Objects, and Singletons"]
B --> C["Why it exists"]
C --> D["How it works in Kotlin"]
D --> E["Android or JVM example"]
E --> F["Mistakes and interview answer"]
Examples
Companion object as a factory
class ApiClient private constructor(val baseUrl: String) {
companion object {
fun create(baseUrl: String) = ApiClient(baseUrl)
}
}
val client = ApiClient.create("https://api.skillupclass.com")
Destructuring a data class
data class Point(val x: Int, val y: Int)
val (x, y) = Point(4, 9)
When to use it
Use it when: Use this to model real domain concepts clearly - a data class for a User or Order, a sealed class for a finite set of UI states, an interface for a contract that several classes share.
Avoid it when: Avoid deep inheritance hierarchies for code reuse alone; prefer composition when the relationship is not a genuine is-a relationship.
Common mistakes
- Using a companion object for everything instead of plain top-level functions
- Marking a function inline without a lambda parameter to justify it
- Making everything public by default instead of choosing the narrowest visibility that works
- Overusing destructuring on data classes whose field order can change
- Reaching for a custom DSL when a simple function call would be clearer
Interview questions
Read each answer in four parts: simple answer, code example, trade-off, and common mistake.
1. What is Objects, Companion Objects, and Singletons in 1.5 Object-Oriented vs Functional Kotlin?
Answer: Objects, Companion Objects, and Singletons is an important Kotlin topic from 1.5 Object-Oriented vs Functional Kotlin. In this lesson we focus only on the concepts that belong together: object, companion object, singleton, factory, static-like access. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.
class ApiClient private constructor(val baseUrl: String) {
companion object {
fun create(baseUrl: String) = ApiClient(baseUrl)
}
}
val client = ApiClient.create("https://api.skillupclass.com")- Trade-off: class and object solve related but different problems: class can have many instances; object always has exactly one.
- Common mistake: Using a companion object for everything instead of plain top-level functions
2. How does Objects, Companion Objects, and Singletons work in Kotlin or Android code?
Answer: Objects, Companion Objects, and Singletons: object, companion object, singleton, factory, static-like access works by giving Kotlin extra control over instance creation, visibility, and compiled output. object and companion object model a single shared instance, visibility modifiers decide what other files or modules can see, and features such as inline, reified, and destructuring remove boilerplate while the compiler still generates ordinary, predictable bytecode underneath.
data class Point(val x: Int, val y: Int)
val (x, y) = Point(4, 9)- Trade-off: class and object solve related but different problems: class can have many instances; object always has exactly one.
- Common mistake: Marking a function inline without a lambda parameter to justify it
3. What is one common mistake with Objects, Companion Objects, and Singletons?
Answer: A common mistake is: Using a companion object for everything instead of plain top-level functions Another is: Marking a function inline without a lambda parameter to justify it
class ApiClient private constructor(val baseUrl: String) {
companion object {
fun create(baseUrl: String) = ApiClient(baseUrl)
}
}
val client = ApiClient.create("https://api.skillupclass.com")- Trade-off: class and object solve related but different problems: class can have many instances; object always has exactly one.
- Common mistake: Using a companion object for everything instead of plain top-level functions
4. How would you explain Objects, Companion Objects, and Singletons with a small example?
Answer: Objects, Companion Objects, and Singletons is an important Kotlin topic from 1.5 Object-Oriented vs Functional Kotlin. In this lesson we focus only on the concepts that belong together: object, companion object, singleton, factory, static-like access. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas. Here is a short example that shows it in practice.
class ApiClient private constructor(val baseUrl: String) {
companion object {
fun create(baseUrl: String) = ApiClient(baseUrl)
}
}
val client = ApiClient.create("https://api.skillupclass.com")- Trade-off: class and object solve related but different problems: class can have many instances; object always has exactly one.
- Common mistake: Marking a function inline without a lambda parameter to justify it
5. What is the trade-off of using Objects, Companion Objects, and Singletons?
Answer: class and object solve related but different problems: class can have many instances; object always has exactly one.
data class Point(val x: Int, val y: Int)
val (x, y) = Point(4, 9)- Trade-off: class and object solve related but different problems: class can have many instances; object always has exactly one.
- Common mistake: Using a companion object for everything instead of plain top-level functions
Practice task
Write one Kotlin example for Objects, Companion Objects, and Singletons. Then explain it using: what is it, how it works, where it is used, mistake to avoid, and one interview answer.