Companion Objects

This Atom is Under Construction

Member functions act on particular instances of a class. Some functions aren’t “about” a particular object, so it doesn’t make sense to tie them to an object.

You could argue that, in this case, you should just make an ordinary function declared at the top level, but it’s more expressive if you can say, “this function or field is about a particular class.” companion objects serve this goal.

End of sample. See AtomicKotlin.com for full early-access book.



Previous          Next

©2018 Mindview LLC. All Rights Reserved.