avatarGabriel Shanahan

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1627

Abstract

c"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FnqceEy1cw&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FnqceEy1cw&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><p id="97ed">Objects are in fact types that have a single instance, both of which are denoted by the same word. This means that <code>MathStuff</code> can represent a type, but it can also represent a value, depending on the context. We’ve actually <a href="https://readmedium.com/types-142c319e5923">already talked about this</a>, when we talked about <code>Unit</code>.</p> <figure id="d54d"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2F68YLNY-V6&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2F68YLNY-V6&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><p id="aa34">Nat

Options

urally, the above code is only to demonstrate how an object can appear both as a type and as a value. Other than that, the above design doesn’t really make much sense — <code>Config</code> is a singleton, so it makes no sense to accept it as a parameter, it should just be accessed directly from within the code:</p> <figure id="89ca"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2F1zsDp3RJQ&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2F1zsDp3RJQ&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><p id="d85b">Go back to <a href="https://readmedium.com/data-classes-97cbd6bfa0b7">Data Classes</a>, jump to the <a href="https://readmedium.com/table-of-contents-c52573cfa291">Table of Contents</a>, or continue to <a href="https://readmedium.com/object-expressions-ce915bf64873">Object Expressions</a>.</p><figure id="8ecd"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*biBSB579iezsNvEQ_NMLBg.png"><figcaption><a href="https://www.etnetera.cz/prace-u-nas?utm_source=medium&amp;utm_medium=GabrielShanahan&amp;utm_campaign=KotlinPrimer&amp;utm_content=join-our-team&amp;utm_term=KotlinPrimer#pozice">Join me in Etnetera</a></figcaption></figure></article></body>

Object Declarations

A quick note on using object declarations to create singletons in Kotlin, and how they represent both a value and a type

— — — — — — — — — — — — — — —

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

— — — — — — — — — — — — — — —

Tags: #KOTLIN FEATURE

This article is part of the Kotlin Primer, an opinionated guide to the Kotlin language, which is indented to help facilitate Kotlin adoption inside Java-centric organizations. It was originally written as an organizational learning resource for Etnetera a.s. and I would like to express my sincere gratitude for their support.

It is recommended to read the Introduction before moving on. Check out the Table of Contents for all articles.

Kotlin makes it very easy to declare singletons:

Objects are in fact types that have a single instance, both of which are denoted by the same word. This means that MathStuff can represent a type, but it can also represent a value, depending on the context. We’ve actually already talked about this, when we talked about Unit.

Naturally, the above code is only to demonstrate how an object can appear both as a type and as a value. Other than that, the above design doesn’t really make much sense — Config is a singleton, so it makes no sense to accept it as a parameter, it should just be accessed directly from within the code:

Go back to Data Classes, jump to the Table of Contents, or continue to Object Expressions.

Join me in Etnetera
Kotlin
Java
Programming
Object Oriented
Singleton
Recommended from ReadMedium