Welcome to SO, first, it's important to reiterate the difference between a class and an object:
- A class is a blueprint or a template for creating objects. It defines the properties and behaviour of objects.
- On the other hand, an object is an instance of a class. It is created using the blueprint or template provided by the class.
Now that's out of the way, you can find decent amount of information of Kotlin Extension functions here:
The documentation states:
you can write new functions for a class or an interface from a third-party library that you can't modify.
Such functions can be called in the usual way, as if they were methods of the original class. This
mechanism is called an extension function. There are also extension properties that let you define new
properties for existing classes.
And if you further looked down:
The this keyword inside an extension function corresponds to the receiver object (the one that is passed
before the dot). Now, you can call such a function on any MutableList:
So the extension function acts on a received object, not the blueprint (class) itself.
System
is a class and the currentTimeMillis()
is a static native method. Therefore, when you try to
create an extension function for it, your extension function will be called on the class itself, not on an
object of the class in your case as you were calling the static method all along from System
.
Next, does your requirements really need an extension function for this? You're calling a static, creating a function should be enough. (i.e. You have all the information you need)
fun stringifedTimeInSeconds() =
System.currentTimeMillis().div(1000).toString()
Finally, it might be worth reading some answers in the following question:
Hope this helps.