O'Reilly logo

Kotlin Programming: The Big Nerd Ranch Guide, First Edition by David Greenhalgh, Josh Skeen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required