facebook

Why Kotlin is used and what is SINGLETON in Kotlin?

By Arindam Kundu

kotlin

Why Kotlin is used and what is SINGLETON in Kotlin?

Kotlin is a statically typed language. It is also known as JVM language because Kotlin needs Java Virtual Machine (JVM) to execute its bytecode.

Some exceptional features making it different than others

i) Since it is JVM language so it’s fully interoperable on Java language. Since it is new in the market so it’s developed with lots of features that new developers for real-world crave for.

ii) It supports to avoid NullPointerException which is a big headache for developers if they get this exception.

iii) It supports Immutability that is you can declare a value inside Kotlin either in values or variables. If they are variables they can change but if they are values then their values are fixed at initialization just like constants.

iv) It is Object Oriented Language just like Java or C++.

v) In Kotlin, you can pass a function as a parameter to another function likely pass of the variable. Even a function can return another function likely return of any values.

vi) It follows less ceremony. It means you use less number of codes to get more results compared to any other languages. Therefore by your code looks much cleaner.

In the case of Singleton, we have just ONE INSTANCE of a class in the whole application. In Kotlin, suppose you have a class named Student which is declared as singleton then we cannot create objects for this class that is there exists only one object for this class by default which is created by Kotlin internally. In simple word, you can say we cannot create objects like student1,student2, etc. In kotlin, you cannot declare ‘static’ variables and methods. When you declare something as object, the koltin internally create a singleton object when the program is running. Example-

object Customer {

var id:Int = -1 //Behaving as Static Variable

fun registerCustomer() { //Behaving as Static Method

//Your code…

}
}

Customer.id=27 //Calling without creating instance

Customer.registerCustomer()

Arindam Kundu Subscriber
Android Developer , Openweb Solutions

Android Developer at Openweb Solutions

Posts created 5

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top
shares