Kotlin

Kotlin Companion Object

The Kotlin programming language is very vast and extensive in terms of the concepts and functionalities that it offers. Therefore, you need to have a thorough understanding of the basics of these concepts before you can master this language. This article will introduce another concept of the Kotlin programming language, i.e., companion object in Ubuntu 20.04.

What is the Companion Object in Kotlin in Ubuntu 20.04?

At times, we might want to access the member functions and variables of a class without creating an object of that class. To achieve this functionality in the programming languages like Java and C++, we make such members of a class static. However, because of the absence of the static keyword in Kotlin, we use a companion object. The companion object allows us to access the member functions and variables within its scope without creating an object of the class. You can call these members simply with the help of the name of the class.

Using the Companion Object in Kotlin in Ubuntu 20.04:

To learn how you can make use of the companion object in the Kotlin programming language in Ubuntu 20.04, you will have to go through the following four examples:

Example # 1: Calling a Function with the Companion Object:

In this example, we just intend to write a very basic Kotlin script that will be capable of calling a function of a class with the class name because of the presence of the companion object. This functionality is shown in the image below:

In this Kotlin script, we have first created a class and named it “CompanionClass.” You can have any name for this class that you want. Then, within this class, we have defined a companion object with the help of the keywords “companion” and “object” and have named it “O1”. Within the body of this object, we have defined a function named “printable()” and inside this function, we have just used a “println” statement for printing a message on the terminal. Then, we have our “main()” function, which we have simply called the “printable()” function with the name of our class without creating any object.

Now, to compile this Kotlin program, we have used the following command:

$ kotlinc companion.kt

Then, to run this Kotlin program, we have executed the command shown below:

$ kotlin CompanionKt

The following image shows the output of this Kotlin script. It implies that you can easily call the function of a class without creating its object in the “main()” function just with the help of the Kotlin companion object.

Example # 2: Calling a Function with the Companion Object without Name:

The companion object in Kotlin can also be used perfectly well just by stating the “companion” and “object” keywords without any proceeding name. The example shown below depicts this:

You can notice that this Kotlin program is exactly the same as the one that we have used in our first example; however, the only difference is that this time, we have not written any name for our companion object. Nevertheless, the target function can still be called in the very same way.

To confirm if the companion object in Kotlin can work properly without any name or not, we executed the script shown above. Upon execution, this script produced the result shown in the following image. You can verify from this image that the Kotlin companion object does not necessarily need a name to operate properly.

Example # 3: Accessing a Variable with the Companion Object:

Apart from calling the functions of a class, you can also access its variables with the class name by using the Kotlin companion object. To show you how this can be done, we have implemented the Kotlin script shown in the image below:

In this Kotlin program, we have used the same “printable()” function within our class that we have used in the two examples discussed above. However, apart from this function, we have also declared a variable “v1” within our class inside the scope of the companion object this time. We have assigned an integer value “10” to this variable. After that, in our “main()” function, we have first called the “printable()” function followed by the variable “v1” of the class with the help of the class name.

The output of this Kotlin script is shown in the following image. This shows that we were able to access the correct value of the class variable just with the help of our class name.

Example # 4: Using the Companion Object for Printing User Data:

Now, we will share an advanced example with you in which we will create a program to display the user data on the terminal. In this program, we will not just call a function of a class with the class name but will also pass some parameters to it. The Kotlin script shown in the image below demonstrates this:

In this example, we have created our class named “CompanionClass.” Then, within the scope of our companion object, we have defined a function named “userData().” This function is capable of accepting two parameters named “name” and “age” of types “String” and “Integer” respectively. Then, within this function, we just wanted to print the values that will be passed to these parameters. After that, in our “main()” function, we have called the “userData()” function with the class name and have passed a name and the corresponding age to it.

The output of this program shown in the following image implies that this Kotlin script also works perfectly well as it has correctly displayed the name and age that were passed to the “userData()” function.

Conclusion:

In this article, we enlightened you about the purpose of the companion object in Kotlin. After that, we shared with you some examples that demonstrated how the companion object could be used in the Kotlin programming language in Ubuntu 20.04. By reading this article, you will quickly grasp the reason behind the existence of the companion object in Kotlin.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.