
164 MAKING THINGS TALK
That’s all the code to define a Player. Put this
code at the end of your program (shown next),
just as if it were another method. To make a new Player
object, write something like Player newPlayer = new
Player(h, v, thisClient).
When you do this, the new Player, and all its instance
variables and methods, are accessible through the variable
called newPlayer (the new Player is not actually stored
in this variable; it’s stuffed away in a portion of memory
somewhere that you can get at through the newPlayer
variable). Keep an eye out for this in the program.
public Player (int hpos, int vpos, Client someClient) {
// initialize the local instance variables:
paddleH = hpos;
paddleV = vpos;
client = someClient;
}
Here’s the constructor for the
Player class. It comes right after the
instance variables in your code. As you
can see, it just takes the values you
give it when you call for a new Player
and assigns them to variables that
belong to an instance (an individual
player) of the class:
8
The variables declared at the beginning of the class as
shown in the example are called instance variables. Every
new instance of the class created makes its own copies
of these variables. Every class has a constructor method.
This method gets called to call the object into existence.
You’ve already used constructors. When you made a
new Serial port in Processing, ...