Companion objects
Java has a static keyword that can be applied to class members. Static members belong to a type, and not to an instance of a type. Kotlin doesn't have a static keyword, but can achieve this kind of functionality with some other language features. A common use case for static functions is grouping some utility functions inside a class. The collections class from the java.util package is a perfect example of this.
You can have this in Kotlin with functions declared at the file level or inside an object.
Another use of static members could be factory methods. This is usually done by hiding the class constructor from the outside with a private access modifier and delegating the instance construction to static methods.
Factory ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access