The object Keyword
In Chapter 13, you learned about constructing classes. A class constructor returns an instance of a class, and you can call the constructor any number of times to create any number of instances.
For example, NyetHack can have any number of players, because Player’s constructor can be called as many times as you would like. For Player, this is desirable, because the world of NyetHack is big enough for multiple players.
But suppose you wanted a Game class to keep track of the state of the game. Having multiple instances of Game would be a problem, because they could each hold their own states, which could potentially get out of sync with each other.
If you need to hold on to a single instance with state that ...
Get Kotlin Programming: The Big Nerd Ranch Guide, First Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.