To call a method or member of a class in any programming language, such as C++, Java, or Kotlin, we use an object of that class. We first create the class object and then call the respective method.
A simple example of calling a method using an object in Kotlin is given below:
In the above code, we create the class TestClass
and then the method testMethod()
inside it. In the main
function, we create the object of TestClass
and then call testMethod()
using this object.
We can call the method of a class using a class name in some programming languages, such as in C# or Java. We declare the methods or members of the class by using the static
keyword, following which we don’t need to create an object to call these methods.
A simple example of calling static and non-static methods in Java is given below:
class TestClass{public static void staticMethod(){System.out.println("I am staticMethod.");}public void nonStaticMethod(){System.out.println("I am nonStaticMethod.");}}class Abc{public static void main(String args[]){// Calling static methodTestClass.staticMethod();// Calling non-static methodTestClass obj = new TestClass();obj.nonStaticMethod();}}
In the above code, we create static and non-static methods in the class TestClass
. In the class Abc
, we call the method staticMethod()
using the class name and the method nonStaticMethod()
using the object of the class.
We can also call the method or member of a class using a class name in Kotlin, but here we do not have the static
keyword. We can use the companion
object here instead.
companion
objectWe call the member of a class using a class name in Kotlin by creating the companion
object as a member inside the class.
A simple example is given below:
class ClassA {companion object CompanionObject {fun testMethod() = println("I am testMethod inside CompanionObject.")}}fun main(args: Array<String>) {ClassA.testMethod()}
We add the companion
keyword in front of the object declaration to create a companion
object. Inside the companion
object, we add the members of the class. These members can be called using the class name. Here we call testMethod()
using the class name.
We can also remove CompanionObject
(the name of the companion
object) as in the example below:
class ClassA {companion object {var name: String = "David"fun testMethod() = println("I am testMethod inside companion object.")}}fun main(args: Array<String>) {println(ClassA.name)ClassA.testMethod()}
In the code above, we remove the name of the companion
object and then call members (variable name
and the testMethod()
method) of a class ClassA
using the class name.
Unlock your potential: Kotlin series, all in one place!
To continue your exploration of Kotlin, check out our series of Answers below:
How to use companion objects in Kotlin
Learn how Kotlin's companion objects enable calling class members without creating an instance, similar to static methods in Java.
How to create a singleton class in Kotlin
Learn how to implement the singleton pattern in Kotlin using the object
declaration for single-instance access to resources.
What are sealed classes in Kotlin?
Learn how sealed classes in Kotlin restrict inheritance to a predefined set, enhancing control and type safety in coding.
What is the purpose of Companion Object in Kotlin?
Learn how Kotlin's companion objects create static properties and functions, centralize shared attributes, and implement factory methods for cleaner, maintainable code.
Data types in Kotlin
Learn how Kotlin categorizes data types into primitive and reference types, supporting numbers, characters, booleans, arrays, strings, classes, functions, and nullable types.
Descending sort of 0s, 1s, and 2s in kotlin
Learn how to modify the Dutch National Flag algorithm in Kotlin to efficiently sort arrays of 0s, 1s, and 2s in descending order with O(N) time and O(1) space complexity.
Free Resources