Instances

In the Data Modeling unit, we learned how to model the state of our game. That is, we learned how to represent the state of the game using the data-modeling mechanisms Java provides: primitives, classes, arrays, and enumerated types. Now we are ready to begin coding the first step in the game cycle: initializing the game state. By “initializing,” we mean “create the first instance of.” But what do we mean by “instance”?

Recall the basic system model:

Basic System Model

An instance of game state is the set of memory locations used by the game state. Let's start with a really simple game, using one die. In this case we can model our game state with a single int, which occupies one memory location.

Instance of One Die

If our game state consists of a pair of dice, modeled with the Dice class we showed before, then what that looks like in memory is shown in Figure Instance of Dice.

Instance of Dice

An instance of the Card class looks like Figure Instance of Card.

Instance of Card

An instance of an array of primitives, like the int[] we saw earlier, looks like Figure Instance of an Array of Primitives.

Instance of an Array of Primitives

Now let's make it complicated. Recall that we modeled a deck of cards as a Card array (Card[]). In this way of representing a deck, each element of the array representing the deck is a Card instance. You might think that it would look like Figure NOT an Instance of a Card Array.

NOT an Instance of a Card Array

But it doesn't. Instances that contain other instances do not contain them directly; they contain references to the other instances. So an instance of Card[] looks like the diagram shown in Figure An Instance of a Card Array.

An Instance of a Card Array

The arrows in the figure represent the references. A reference is actually the address of the instance it refers to. If you were to inspect the contents of index 0 in the array representing the deck, it would contain the address—the numeric location in memory—of the instance representing the Ace of Clubs card.

An instance of Tic Tac Toe, which we modeled as TicTacToeSquare[][] earlier, looks similar, and is shown in Figure Instance of Tic Tac Toe.

Instance of Tic Tac Toe

Instead of an array whose elements are references to Card instances, in Tic Tac Toe we have an array whose elements are references to other arrays.

Objects

Instances of class and array types are referred to as objects, and any class or array type is known as a reference type, because their instances are accessed via references.