Singleton/Top-level declaration/ Module


#1

Hi,

What is Signleton? Top-level declaration? Module? (In Kotlin)

Hold on now! I know its a very basic question but i’m at the end of a very poor Kotlin course and I want to finish it then follow another course with a better instructor from A-Z Again.

Please don’t compare them with java because I don’t know anything about java.

Thank you.


#2

A singleton is a class from which only a single instance can be created. In Kotlin singletons are defined using the ‘object’ keyword rather than ‘class’.

A top level declaration is a function or property which is defined outside any class, object, interface or other structure.

A module is a set of Kotlin files which are compiled together as a single project.


#3

Thank u for your reply…

  • So Singleton is a type of an object?
  • What do you mean by ’ out side of class… ’

#4

Normally, when you have a class you can create as many instances of it as you like. However, in the case of a singleton, only one instance is possible. Kotlin uses the ‘object’ keyword to signify a singleton.

A class or object can contain several member functions or properties. In the case of a top level function or property they just stand on their own - they don’t belong to a particular class or object.


#5

Thank u again
I got it


#6

Btw I moved this from #native to #support . The #native category is used for discussions/questions about Kotlin Native, which is a version of Kotlin not compiling to JVM bytecode (java) but instead directly to machine code.
I guess most of your questions being new should either be in the #support or the #language-design category here :wink:


#7

No problem
will do that next time