Definitions
Using objects
Creating a name
Assigning an object to a name
Using an object
Defining a class
Instance variables
Constructor methods
Instance methods
Note the following definitions.
This is simply declaring a variable of the given type.
There is no difference between this statement and a statement like ``int i.'' Here, we are creating a variable called rob to be a name for a Robot object.Robot rob;
We haven't actually made it be a name for any particular object yet. In fact, rob will hold null.
Or we could create a name for a DrawableFrame object.
DrawableFrame window;
To assign an object to a name, we'll want to create the object.
Here, we're changing what object the rob name is naming. In this case, the object we're making it be a name for is a new Robot object created on the same line.window = new DrawableFrame(); rob = new Robot(window, 100, 100);
The same object can have multiple names. For example, the following code would create a second name bert for rob.
Robot bert; bert = rob;
The new keyword is used when you want to create an instance. The way it works is as follows.
Each object has several methods which specify what we can ask of that object. For example, a Robot object might have the following methods. (This is how they might be described in the documentation.)
To tell a Robot object to accomplish one of these behaviors, we would give the name of a Robot, followed by a period and the method name, with a set of parentheses. Any parameters required by the method would go inside the parentheses.
Here, we have told rob to move forward 50 pixels. Then we told rob to turn left. (Note that when we told rob to turn left, we still had to include the parentheses, even though we had no parameter values to give him.)rob.moveForward(50); rob.turnLeft();
Many methods can return a value - this would be the object's response to the request. You can tell in the documentation that it returns something when there is a type name other than void listed before the method name. For example, the getY() method returns an int. We can put such a method call anywhere we can place an expression. For example, we might put the return value into a variable.
Here, we've asked rob for his y-coordinate, and then we've told rob to move that far forward. We could combine these into a single line to accomplish the same thing.int y; y = rob.getY(); rob.moveForward(y);
rob.moveForward(rob.getY());
When we design our own systems, we define our own new classes. For example, we might want to define the Robot type. Beginning the class definition is simple.
public class Robot { // : (everything else goes between these two braces) }
The first question we ask about a new class is: What does it need to do? In the case of the Robot class, we've already said what it needs to do.
The second question we need to ask is: What does it need to remember in order to accomplish this feat? Each thing that the object remembers is an instance variable. (This distinguishes it from a local variable, which is something that is remembered only for the duration of executing a method.)
What do robots need to remember? Each will need to remember its x- and y-coordinates, which window it is living within, and which direction it is facing.
public class Robot { private DrawableFrame window; private int x; private int y; private int direction; // 0 for east, 1 north, 2 west, 3 south // : (more to come here) }
When we create a Robot object, its constructor method is called. Within our class definition, we need to define what the method does. In particular, the method should initialize all the instance variables to their proper values when a new robot is created.
public class Robot { private DrawableFrame window; private int x; private int y; private int direction; // 0 for east, 1 north, 2 west, 3 south public Robot(DrawableFrame in_window, int in_x, int in_y) { window = in_window; x = in_x; y = in_y; direction = 0; } // : (more to come here) }
Notice that we have not done the following.
In this example, we have declared local variables within the constructor method whose names just happen to be identical to the instance variable names. But this coincidence of names is of no significance to Java; it thinks of them as being totally different variables, and as a result, the instance variables are not initialized with this technique.public class Robot { private DrawableFrame window; private int x; private int y; private int direction; // 0 for east, 1 north, 2 west, 3 south public Robot(DrawableFrame in_window, int in_x, int in_y) { DrawableFrame window = in_window; // BAD!!! int x = in_x; // BAD!! int y = in_y; // BAD!! int direction = 0; // BAD!! } }
We also need to define how the robot handles the various messages. Each method definition is a sequence of statements that the robot performs when requested. These statements may refer to the instance variables or call instance methods.
Notice how moveForward() declared some local variables - you know they're local, since they're defined inside the method. (If they were declared outside the method, they'd be instance methods.) These are temporary variables, since we only want to remember them while the robot is moving forward. We don't really have any reason for the robot to remember them later, so we make them local variables instead.public class Robot { private DrawableFrame window; private int x; private int y; private int direction; // 0 for east, 1 north, 2 west, 3 south public Robot(DrawableFrame in_window, int in_x, int in_y) { window = in_window; x = in_x; y = in_y; direction = 0; } public int getX() { return x; } public int getY() { return y; } public void moveForward(int dist) { int init_x = x; int init_y = y; if(dist == 0) { x += dist; } else if(dist == 1) { y -= dist; } else if(dist == 2) { x -= dist; } else if(dist == 3) { y += dist; } Graphics g = window.getGraphics(); g.drawLine(init_x, init_y, x, y); } public void turnLeft() { dist++; if(dist == 4) dist = 0; } }