Share
Explore

Android Mojo: The Companion Object

Lecture Note: Companion Objects in Kotlin

What is a Companion Object?

In Kotlin, a companion object is a special construct that allows you to define members (such as constants, methods, or properties) that belong to a class, rather than any specific instance of the class. It essentially functions like the concept of static members in Java.

Syntax

Here’s the basic syntax of a companion object:
class MyClass {
companion object {
const val CONSTANT_VALUE = "Hello, World!"
fun staticMethod() {
println("Called from the companion object")
}
}
}

To access the members of the companion object, you don’t need an instance of the class:
println(MyClass.CONSTANT_VALUE) // Outputs: Hello, World!
MyClass.staticMethod() // Outputs: Called from the companion object

Why Do We Use a Companion Object in MainActivity?

In MainActivity.kt, we used a companion object to define the constant:
companion object {
private const val LOCATION_PERMISSION_REQUEST_CODE = 1
}

Why Use a Companion Object Here?

Avoid Redundancy Across Instances:
The constant LOCATION_PERMISSION_REQUEST_CODE is not specific to any instance of MainActivity. It is meant to be shared across all instances.
A companion object ensures that the constant is only stored once in memory, no matter how many instances of MainActivity are created.
Improves Readability and Maintenance:
Instead of hardcoding the value 1 throughout the class, the constant is defined in one place. If we need to change the value, we update it in one place.
Works Well with Android Framework:
Android’s onRequestPermissionsResult method requires a requestCode to identify which permission request has been completed. Using a companion object ensures that this code is consistent and globally accessible.

Companion Object vs Static in Java

Table 1
Feature
Companion Object in Kotlin
Static in Java
1
Belongs to Class
Yes
Yes
2
Multiple Declarations
No, only one per class
Yes, multiple static fields/methods allowed
3
Naming
Can be named explicitly
Always uses static keyword
There are no rows in this table
Example:
class Example {
companion object MyCompanion {
fun printMessage() {
println("Hello from companion object!")
}
}
}

fun main() {
Example.printMessage() // Kotlin-style access
Example.MyCompanion.printMessage() // Explicit name usage
}

Real-Life Analogy

Think of a companion object like a guidebook shared among all members of a group. Every member can refer to it for consistent, reliable information, and it doesn’t belong to any one person—it’s common to all.

Recap: Why Are We Using It Here?

LOCATION_PERMISSION_REQUEST_CODE is static-like:
It doesn’t belong to any specific instance of MainActivity.
A companion object provides a clean way to define such shared members in Kotlin.
Better Code Design:
Centralized constants reduce redundancy and make the code easier to maintain.
Android-Specific Requirement:
The onRequestPermissionsResult method requires requestCode to distinguish between different permission requests, and using a constant ensures consistency.

Discussion/Quiz Questions

Why do we use a companion object instead of a regular property for LOCATION_PERMISSION_REQUEST_CODE?
Can we have multiple companion objects in a single class? (Hint: No.)
How does a companion object differ from Java's static keyword?

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.