<p>This tutorial is about companion objects in Kotlin with the help of examples.</p>



<p class="p1">Kotlin doesn&#8217;t have a ;<span style="color: #008000;"><strong>static</strong></span> keyword. Static members belong to a type, and not to an instance of a type.</p>



<p class="p1">In kotlin to achieve this static feature, we make use of <span style="color: #0000ff;"><strong>companion object</strong></span>.</p>



<h3 class="wp-block-heading"><span style="color: #000080;"><strong>companion object</strong></span></h3>



<p class="p1">An object declaration inside a class can be marked with the ;<span style="color: #008000;"><strong>companion</strong></span> keyword known as <span style="color: #0000ff;"><strong>companion object</strong></span>. There can only be ;<span style="color: #0000ff;"><strong>one companion object</strong></span> in a class and it ;is ;<span style="color: #008000;"><strong>initialized</strong></span> ;when the ;<span style="color: #0000ff;"><strong>class is loaded</strong></span>.</p>



<p><span style="color: #0000ff;"><strong>Example:</strong></span></p>



<pre class="wp-block-preformatted">class User {
 companion object Test {
 fun show() = println("I'm inside companion object")
 }
}</pre>



<ul class="wp-block-list"><li><span style="color: #000000;">Here,</span><strong><span style="color: #0000ff;"> Test</span></strong> object declaration is marked with keyword <strong><span style="color: #0000ff;">companion</span></strong> ; to create a companion object.</li></ul>



<p>Members of the companion object can be called by using simply the class name as the qualifier.</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>




<pre class="wp-block-preformatted">fun main() {
 User.show()
<strong>//or</strong>
 User.Test.show()
}</pre>



<p><span style="color: #0000ff;"><strong>Output:</strong></span></p>



<pre class="wp-block-preformatted">I'm inside companion object
I'm inside companion object</pre>



<p class="p1">The name of the companion object can be omitted and if the companion object name is missing, the default name <span style="color: #008000;"><strong>Companion</strong></span> ;is assigned to it.</p>



<pre class="wp-block-preformatted">class User {
 companion object {
 fun show() = println("I'm inside companion object")
 }
}

fun main() {
 User.show()
 User.Companion.show()
}</pre>



<p>Similar to normal objects, companion objects cannot have constructors, but can extend other classes and implement interfaces.</p>



<p><span style="color: #0000ff;"><strong>Example:</strong></span></p>



<p><span style="color: #008000;"><strong>companion object</strong></span> implementing interface <span style="color: #008000;"><strong>Runnable</strong></span>.</p>



<pre class="wp-block-preformatted">class User {
 companion object : Runnable {
 override fun run() {
 println("run method invoked")
 }
 }
}

fun main() {
 User.run()
<span style="color: #008000;"><strong>//or</strong></span>
 User.Companion.run()
}</pre>



<p><span style="color: #0000ff;"><strong>Output:</strong></span></p>



<pre class="wp-block-preformatted">run method invoked
run method invoked</pre>


