November 2018
Intermediate to advanced
388 pages
9h 5m
English
Static functions are functions that can be invoked without creating multiple instances of a class. Static functions avoid code duplication and can be reused.
Let's take a look at how to write a static function in Kotlin.
Consider the following code for 15a_StaticMethods.kts:
object MyUtil { fun foo(){ println("Static function foo() is invoked") }}MyUtil.foo()
Note that we declared an object MyUtil and defined a function foo(). This is known as object declaration.
We invoked the function foo() directly using the object MyUtil.
The output of the preceding code is as follows:

There are different ways to write static functions ...
Read now
Unlock full access