Know about Kotlin Companion Object and Static Keyword

In other programming language like Java and C#, when we want to call the method and want to access the members of a class then we make the object of the class and with the help of that object, we access the members of the class. like:-

class ToBeCalled {
    fun callMe() = println("Codinglance:)")
}
fun main(args: Array<String>) {     
    val obj = ToBeCalled()
    
    // calling callMe() method using object obj
    obj.callMe()
}

The static keyword

In some other  languages like Java and C#, they have static keyword to declare the members of the class that can be use without making any object means you can call them with the help of class name.

public class MyClass {

    public static void myMethod() {
        // do something
    }

}

Here we can call myMethod without making instance:-MyClass.myMethod();

In kotlin to use this we use a companion object

Companion object

In Kotlin, if you want call a function or any member function without making the instance of the class then you can use companion object for that you have to write function or any member function  as a member of a companion object inside the class. So, by declaring the companion object, we can easily access the members of the class by class name only.

To create a companion object in kotlin, you just need to add the companion keyword in front of the object declaration.

class CompanionClass {

    companion object {

    }
}

val obj = CompanionClass.Companion

So, here is the example of a companion object in Kotlin:

class ToBeCalled {
    companion object Test {
        fun callMe() = println("Codinglance :)")
    }
}
fun main(args: Array<String>) {
    ToBeCalled.callMe()
}

The output of the above code is “ Codinglance 🙂 ”

Object Declarations

Singleton is an object-oriented pattern where a category can have just one instance (object).
For example, you’re working an application having SQL database backend. you wish to form a connection pool to access the database while reusing the identical connection for all clients. For this, you’ll be able to create the connection through singleton class in order that every client get the identical connection.
Kotlin provides a simple thanks to create singletons using the item declaration feature. For that, object keyword is employed.

object SingletonExample of sophistication
... .. ...
}

Object Expressions

Object expressions create objects of anonymous classes, that is, classes that are not explicitly declared with the category declaration. Such classes are handy for one-time use. you’ll be able to define them from scratch, inherit from existing classes, or implement interfaces. Instances of anonymous classes also are called anonymous objects because they’re defined by an expression, not a name.

window.addMouseListener(object : MouseAdapter() {
    override fun mouseClicked(e: MouseEvent) { /*...*/ }

    override fun mouseEntered(e: MouseEvent) { /*...*/ }
})

You may also like...

0 Comments

No Comment.