In reading documentation on Kotlin I often misinterpret the intent because of the word “object” in the text.
I never read this as “a singleton”. To me (with my Java background) just about everything is an Object.
An example of my struggle: If an object in Kotlin is a singleton, what is an instance of a class?
It’s also taken me quite a while to come to terms with a companion object. The way I understand it is as follows :
A companion object is a singleton that you create inside your class so that you can reference members on that singleton as if they were static members on your actual class.
The name of this singleton object defaults to Companion, however, you can reference the members of the Companion object directly without using Companion. In fact, you can declare the companion object without a name at all.
It still feels strange though to create an object inside my object (not singleton ) (that won’t be referenced) so that I can emulate the behaviour of a static member in my class.
I’d really like to understand what led to this design?
Well very often object is not a singleton; check the documentation
object is a keyword to declare an object inline, but if, for example, that’s in a loop or inside a function you can create very many objects with a single object “call”.
Well that’s because you’re used to the concept of “static” memebers. Kotlin doesn’t have the concept at all. Which can be “emulated” like this with one fewer abstraction
Wow, I had no idea “object” could be used as an expression like that. Does it still generate the “singleton” bytecode under the hood though? That is if I create an anonymous object using object in a loop, am I creating multiple pointers to multiple instances of an object that contains singleton code? Or does Kotlin only generate the singleton code if I use the object keyword in the definition of a non-anonymous class or with the companion object keyword?
Put another way (sorry, trying to get my head around this). If I use the object keyword to generate an “inline” object, does it still create a singleton class behind the scenes to support it?
I think you needlessly think about singletons and that actually makes it more confusing to you. class creates a type which we can use to instantiate objects. object creates an object (instance) directly. This is as simple as that. If you use object once, then this is like a singleton, because you created a single object. If you use it multiple times, you get multiple instances.
Singleton is a design pattern, for me it is a higher level concept. Language is more low-level, it provides basic building blocks like classes, objects, members, etc. and we can use them to construct high-level concepts like a singleton.
Thanks broot. I think you are right. All the documentation I’ve read referring to object always refers to or references singleton and I guess that’s why I thought of it as a singleton, not simply as a way to define and instantiate an object.
I am still struggling with the use case for an object (other than to emulate a singleton or recreate static members of a class).
I guess it could be used to achieve a version of Java’s Anonymous class, although more flexible in that there is no need to reference a parent class or interface.
I will continue my learning. Thank you for guidance.