codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎯

Kotlin

17 / 68 topics
11Extensions Functions12Classes and Objects13Inheritance in Kotlin14Interfaces in Kotlin15Data Classes16Sealed Classes17Companion Objects18Generics in Kotlin19Type Aliases20Properties and Fields21Lateinit Properties22Delegated Properties
Tutorials/Kotlin/Companion Objects
🎯Kotlin

Companion Objects

Updated 2026-05-15
10 min read

Companion Objects

Introduction

In object-oriented programming, static members are properties or methods that belong to the class itself rather than any specific instance of the class. Kotlin provides a way to define these static members using companion objects. Companion objects allow you to have a single instance of an object associated with a class, which can hold static-like properties and methods.

Concept

A companion object in Kotlin is defined within a class and is marked with the companion keyword. It acts as a singleton instance that is tied to the class itself. You can access members of a companion object using the class name directly, without needing to create an instance of the class.

Key Points:

  • Singleton: Each class can have only one companion object.
  • Static-like behavior: Members of the companion object can be accessed using the class name.
  • Named Companion Objects: You can give a companion object a name if needed, but it is optional.

Examples

Let's explore some examples to understand how companion objects work in Kotlin.

Basic Example

Here’s a simple example where we define a companion object with a static property and a static method:

Kotlin
1class MathOperations {
2 companion object {
3 val PI = 3.14159
4 fun square(number: Int): Int {
5 return number * number
6 }
7 }
8}
9
10fun main() {
11 println(MathOperations.PI) // Accessing the static property
12 println(MathOperations.square(5)) // Accessing the static method
13}
Output
3.14159
25

In this example, PI is a static property and square is a static method of the MathOperations class. We access them using the class name MathOperations.

Named Companion Object

You can also give a companion object a name if you need to differentiate it from other objects or for better readability:

Kotlin
1class User {
2 companion object Factory {
3 fun createGuest(): User {
4 return User()
5 }
6 }
7}
8
9fun main() {
10 val guest = User.Factory.createGuest()
11}

In this case, the companion object is named Factory, and we access its method using User.Factory.createGuest().

Companion Object as a Factory

Companion objects are often used to create factory methods that help in instantiating objects:

Kotlin
1class Product {
2 val name: String
3 val price: Double
4
5 companion object {
6 fun createProduct(name: String, price: Double): Product {
7 return Product(name, price)
8 }
9 }
10
11 constructor(name: String, price: Double) {
12 this.name = name
13 this.price = price
14 }
15}
16
17fun main() {
18 val product = Product.createProduct("Laptop", 999.99)
19}

Here, the createProduct method in the companion object acts as a factory to create instances of the Product class.

Accessing Companion Object Members

You can access members of a companion object using either the class name or the companion object instance:

Kotlin
1class Database {
2 companion object {
3 val url = "http://example.com"
4 fun connect() {
5 println("Connecting to $url")
6 }
7 }
8}
9
10fun main() {
11 println(Database.url) // Accessing using class name
12 Database.connect() // Accessing using class name
13
14 val dbInstance = Database.Companion
15 println(dbInstance.url) // Accessing using companion object instance
16 dbInstance.connect() // Accessing using companion object instance
17}

In this example, both the class name and the companion object instance can be used to access its members.

What's Next?

Now that you have a good understanding of companion objects in Kotlin, you might want to explore extensions. Extensions allow you to add new functionality to existing classes without modifying their source code. This is particularly useful for adding utility functions or enhancing third-party libraries. Stay tuned for the next section on extensions!

Info

Remember, companion objects provide a way to define static members in Kotlin, making your code more organized and readable.


PreviousSealed ClassesNext Generics in Kotlin

Recommended Gear

Sealed ClassesGenerics in Kotlin