kotlin companion object

Companion object in kotlin

Companion Objects are similar to Scala

Create a companion object inside a class

A way of adding static methods to a class

Factory pattern: private constructor in class + factory method in companion object TODO: Document rules about relationship between a class and its companion object

Syntax

From Kotlin Language Reference :

  1. Give the companion object a name:

class MyClass {

companion object Factory {

fun create(): MyClass = MyClass()

}

}

val instance = MyClass.create()

2. No name on companion object:

class MyClass {

companion object { } // will be called “Companion”

}

fun MyClass.Companion.foo() { … }

Note that, even though the members of companion objects look like static members in other languages, at runtime those are still instance members of real objects, and can, for example, implement interfaces:”

Leave a Reply

Your email address will not be published. Required fields are marked *