Previous | Next | Trail Map | Writing Java Programs | Java Objects


Using an Object

Once you've created an object, you will very likely want to use it for something. Suppose, for example, that in the user of your drawing application clicked the mouse over the rectangle and dragged it to a new location. Your drawing application should keep pace and update the x, y position of the memory representation of the on-screen rectangle.

The Rectangle class provides two equivalent ways to do this:

  1. call the move() method
  2. manipulate the object's x, y variables directly
Option 1 is often considered "more object-oriented" and safer because you manipulate the object's variables indirectly through its protective layer of methods rather than twiddling directly with its variables. Manipulating an object's variables directly is often considered error-prone; you could potentially put the object into an inconsistent state. However, a class would not make its variables available for direct manipulation by other objects if it were possible for those manipulations to put the object in an inconsistent state. So, we can assume that manipulating a Rectangle's x and y variables directly is as safe as calling its move()method.

A class can restrict or allow access to its instances' variables and methods by other objects. The following two sections discuss calling methods and manipulating variables that have been made accessible to other objects. To learn more about specifying access to an class's methods and variables refer to Missing Page.

Calling an Object's Method

First, let's focus on calling the rectangle's move() method. The next section will show you how to manipulate the Rectangle's x and y variables directly.

To call an object's method, simply append the method name to an object reference with an intervening '.' (period), and provide any arguments to the method within enclosing parentheses. If the method does not require any arguments, just use empty parentheses.

object.method(argumentList);
   or
object.method();
Back to the drawing application, if the user moved the rectangle named rect to a new location, say 244, 47, the call to rect's move() method would look like this:
rect.move(244, 47);
This Java statement calls rect's move() method with two integer parameters, 244 and 47. This statement has the effect of moving the rect object to the x, y location 244, 47. If you wanted to move a different rectangle, named bob for example, to a new location you would write:
bob.move(244, 47);
As you see from these examples, method calls are directed at a specific object; the object specified in the method call is the object that responds to the instruction. Method calls are also called messages. Just like real-world messages, object messages must be addressed to a particular recipient. You get different results depending on which object is the recipient of the message. In the example above, when you send the object named rect a move message, rect moves to the new location. When you send the object named bob a move message, bob moves. Very different results.

Like other Java statements, a method call is an expression and evaluates to some value. The value of a method call is its return value, if it has one. You will often wish to assign the return value of a method to a variable or use the method call within the scope of another expression or statement. The move() method doesn't return a value (it's declared void). However, Rectangle's inside() method does. The inside() method takes an x, y coordinate and returns true if that point lies within the rectangle. So you could use the inside() method to determine if some point, say the current mouse location, was inside the rectangle and do something special if it were.

if (rect.inside(mouse.x, mouse.y)) {	// mouse is in the rectangle
    . . .
} else {				// mouse is outside of the rectangle
    . . .
}
Remember that the method call is like a message to the object named. In this case, the object named is the Rectangle named rect. Thus,
rect.inside(mouse.x, mouse.y)
is asking rect if the mouse cursor location represented by mouse.x and mouse.y is contained within it. You would likely get a different response if you sent the same message to bob.

As stated previously, the object in the method call object.method() must be an object reference. You are not limited to using the object's name here (if it even has one), you can use an expression to determine the object to which to send a message. One common use of an expression as object reference is just after an object is created. For example, you can create an object and immediately thereafter call one of its methods:

new Rectangle(0, 0, 100, 50).equals(anotherRect)
The expression new Rectangle(0, 0, 100, 50) evaluates to an object reference that refers to a Rectangle object. Thus you can use the dot notation to then call the new rectangle's equals() method to determine if the new rectangle is equal to the one specified in equals()'s argument list.

Referencing an Object's Variables

You reference an object's variables in the same way that you call its methods--simply append the variable name to an object reference with an intervening '.' (period).
object.variable
You can use this notation to either view or modify an object's variables. For example, we can change rect's height and width using the following two statements:
rect.height = 15;
rect.width = 37;
Or you can calculate the rectangle's area using these statements
area = rect.height * rect.width;
Similar to method calls, when you reference a variable through an object, you are referencing a particular instances variables. If bob and rect are both rectangles of different height and widths, this instruction
area = rect.height * rect.width;
calculates the area of the rectangle named rect and this instruction
area = bob.height * bob.width;
calculates the area of the rectangle named bob.

As with method calls, the object in object.variable must be an object reference. You are not limited to using just the object's name here (if it even has one), you can use an expression to specify the object whose variables you want to inspect. One common use of an expression as object reference is just after an object is created. For example, you can find out the height of an uninitialized Rectangle with:

height = new Rectangle().height;


Previous | Next | Trail Map | Writing Java Programs | Table of Contents