https://kotlinlang.org logo
Title
m

miha-x64

04/19/2017, 9:08 AM
Companion is a singleton. Static methods are visible through subclasses, companion objects’ methods are not. Static methods are procedural, companions are object-oriented.
m

mg6maciej

04/19/2017, 9:09 AM
I'm not sure there is any use of static methods being visible through subclasses except for confusing readers of the code using them like that.
m

miha-x64

04/19/2017, 9:14 AM
There may be some utility static methods, but one may think they are overridable, which is confusing.
m

mg6maciej

04/19/2017, 9:15 AM
Ok. There is one use: having your own subclass of assertj's
Assertions
and *-importing it to get all functions.
m

miha-x64

04/19/2017, 9:16 AM
First way (subclassing) is similar to declaring constants in interface and implementing it (so constants becoming visible through class which breaks encapsulation). Importing is better, and Kotlin allows it too.