<p class="editor-contents__short-description">This tutorial is about object declarations (singletons) and object expressions with the help of examples.</p>



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



<p>Object declarations defines singletons. Singleton is an object-oriented pattern where a class can have only one instance (object) at a time.</p>



<ul class="wp-block-list"><li>Kotlin creates singletons with the <span style="color: #008000;"><strong>object</strong></span> keyword.</li><li>The object declaration can have <strong><span style="color: #008000;">functions</span></strong>, <strong><span style="color: #008000;">properties</span></strong>, and the <span style="color: #008000;"><strong>init</strong></span> block.</li><li>We can use the <strong><span style="color: #008000;">init</span></strong> block if some initialisation is required. The init block will be called when your object gets constructed.</li><li>Object declaration&#8217;s initialisation is thread-safe and done at first access.</li><li>The constructor method is not allowed in an object.</li><li>To access methods and properties of object, use the object declaration&#8217;s name and dot (.) notation.</li></ul>



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



<pre class="wp-block-preformatted"><strong><span style="color: #008000;">//creating object Test</span></strong>
object Test {

<span style="color: #008000;"><strong>//init block</strong></span>
 init {
 println("Singleton class invoked.")
 }

 var a: Int = 10
 var b: Int = 20

 fun add(): Int = a + b
}

fun main() {
 <strong><span style="color: #008000;">//accessing members using object name</span></strong>
 println("Addition of ${Test.a} and ${Test.b}: ${Test.add()}")
}</pre>



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



<pre class="wp-block-preformatted">Singleton class invoked.
Addition of 10 and 20: 30</pre>



<p>Objects can also implement <strong><span style="color: #0000ff;">interfaces</span></strong> and extend other <strong><span style="color: #0000ff;">classes</span></strong> in a similar way like normal classes.</p>



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



<pre class="wp-block-preformatted"><span style="color: #008000;"><strong>//object RunnableSingleton implementing interface Runnable</strong></span>
object RunnableSingleton : Runnable {
 override fun run() {
 println("I'm a runnable singleton")
 }
}</pre>



<p>Kotlin also allows objects to be declared inside classes. Nested objects cannot access members from the outer class.</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"><span style="color: #008000;"><strong>//declare an object Singleton inside a class Outer</strong></span>
class Outer {
 object Singleton {
 }
}</pre>



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



<p class="p1">The <span style="color: #0000ff;"><strong>object</strong></span> keyword can also be used to create objects of an anonymous class known as <span style="color: #008000;"><strong>anonymous objects</strong></span>.</p>



<p class="p1">They are used if you need to create an object of a slight modification of some class or interface without declaring a subclass for it.</p>



<h5 class="wp-block-heading"><strong><span style="color:#520599" class="has-inline-color">Anonymous object implementing an Interface</span></strong></h5>



<p>Let&#8217;s create an anonymous object of a <strong>Runnable</strong> interface:</p>



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



<pre class="wp-block-preformatted">Thread(object : Runnable {
 override fun run() {
 println("I am anonymous object")
 }
}).run()</pre>



<p>If you need a name for your anonymous object, or need it to store for later use, you can initialise a variable with it.</p>



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



<pre class="wp-block-preformatted">val runnable = object : Runnable {
 override fun run() {
 println("I'm created with anonymous object")
 }
}</pre>



<h5 class="wp-block-heading"><strong><span style="color:#520599" class="has-inline-color">Anonymous object implementing a class</span></strong></h5>



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



<pre class="wp-block-preformatted">open class Vehicle {

 fun start() {
 println("Vehicle is in start mode.")
 }

 fun stop() {
 println("Vehicle is in stop mode.")
 }

 open fun park() {
 println("Vehicle is in park mode.")
 }

}

fun main() {
 val car = object : Vehicle() {
 override fun park() = println("Car is in park mode.")
 }

 car.start()
 car.stop()
 car.park()
}</pre>



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



<pre class="wp-block-preformatted">Vehicle is in start mode.
Vehicle is in stop mode.
Car is in park mode.</pre>



<p class="p1">Here, anonymous object is stored in variable <span style="color: #008000;"><strong>car</strong></span> which implements <strong><span style="color: #008000;">Vehicle</span></strong> class with <strong><span style="color: #008000;">park()</span></strong> method is overridden.

