Software Development

Kotlin – create static fields and methods

This post demonstrates how to create static class members similar to that of Java. We use Kotlin 1.1 here.

All static members Class

If we want to create a class that only contains static members, we use singletons in Kotlin. For example:

[wp_ad_camp_1]

Outputs:

Static and non-static members in class

[wp_ad_camp_2]

Sometimes we have classes that have both static and non-static class members in Java. In Kotlin, we can mix static and non-static members in one class. For example:

Outputs:

[wp_ad_camp_3]

References

https://kotlinlang.org/docs/reference/classes.html

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like