Previous | Next | Trail Map | Learning the Java Language | Objects and Classes in Java

Using Objects

Once you've created an object, you probably want to use it for something. You may need information from it, want to change its state, or have it perform some action.

Objects give you two ways to do these things:

  1. Manipulate or inspect its variables.
  2. Call its methods.
Ideal object-oriented programming discourages the direct manipulation of an object's variables; you could potentially put the object into an inconsistent state. Instead, an ideal object provides methods through which you can inspect or change its state. These methods ensure that the object never gets into an inconsistent state. However, in practical situations, it sometimes makes sense to use an object's variables directly.

Both the Point class and the Rectangle class allow free access to their member variables. You cannot put a Point object in an inconsistent state by setting x or y directly, and you cannot put a Rectangle object in an inconsistent state by setting width, height, or origin.

Java provides an access control mechanism whereby classes can restrict or allow access to its variables and methods. A class should protect variables against direct manipulation by other objects if those manipulations could endanger the object's state. State changes should then be affected and therefore controlled by method calls. If an object grants access to its variables, you can assume that you can inspect and change them without adverse effects. To learn more about Java's access control mechanism, refer to Controlling Access to Members of a Class.

So, back to the Rectangle object. Suppose a Rectangle object represents a rectangular object in a drawing program and the user just dragged it to a new location. You need to update the Rectangle object's point of origin. The Rectangle class provides two equivalent ways of doing this:

  1. Manipulate the object's origin variable directly.
  2. Call the move method.
Rectangle's origin member is accessible to other classes (it's declared public), so you can assume that manipulating a Rectangle's origin member directly is safe.

Referencing an Object's Variables

This section focuses on how to move the Rectangle by modifying its origin variable directly. The next section shows you how to move the rectangle by calling the move method.

Assume you created a rectangle named rect as described in the previous section. To move rect to a new location, you would write:

rect.origin = new Point(15, 37);
This statement moves the rectangle by setting its point of origin to a new position. rect.origin is the name of rect's origin variable. You can use these kinds of object variable names in the same manner as you use other variables names. Thus, as in the previous example code, you can use the = operator to assign a value to rect.origin.

The Rectangle class has two other variables-- width and height-- that are accessible to objects outside of the class. You can use the same notation to access them and calculate the rectangle's area using this statement (or you could just call the area method):

area = rect.height * rect.width;
In general, to refer to an object's variables, append the name of the variable to an object reference with an intervening period (.):
objectReference.variable
The first part of the variable's name, objectReference, must be a reference to an object. You can use an object name here just as was done in the previous examples with rect. You also can use any expression that returns an object reference. Recall that the new operator returns a reference to an object. So you could use the value returned from new to access a new object's variables:
height = new Rectangle().height;
This statement creates a new Rectangle object and immediately gets its height. Effectively, the statement calculates the default height of a Rectangle. Note that after this statement has been executed, the program no longer has a reference to the Rectangle that was created because the program never stored the reference in a variable. Thus the object becomes eligible for garbage collection.

Here's a final word about accessing an object's variables to clear up a point of some confusion that beginning Java programmers often have. All objects of the same type have the same variables. All Rectangle objects have origin, width, and height variables that they got from the Rectangle class. When you access a variable through an object reference, you reference that particular object's variables. Suppose that bob is also a rectangle in your drawing program and it has a different height and width than rect. The following instruction calculates the area of the rectangle named bob, which differs from the previous instruction that calculated the area of rect:

area = bob.height * bob.width;

Calling an Object's Methods

To move rect to a new location using its move method, you write this:
rect.move(15, 37);
This Java statement calls rect's move method with two integer parameters, 15 and 37. It moves the rect object because the rect method assigns new values to origin.x and origin.y and is equivalent to the assignment statement used previously:
rect.origin = new Point(15, 37);
The notation used to call an object's method is similar to that used when referring to its variables: You append the method name to an object reference with an intervening period (.). Also, you provide any arguments to the method within enclosing parentheses. If the method does not require any arguments, use empty parentheses.
objectReference.methodName(argumentList);
   or
objectReference.methodName();
As stated previously in this lesson, objectReference must be a reference to an object. You can use a variable name here, but you also can use any expression that returns an object reference. The new operator returns an object reference, so you can use the value returned from new to call a new object's methods:
new Rectangle(100, 50).area()
The expression new Rectangle(100, 50) returns an object reference that refers to a Rectangle object. As shown, you can use the dot notation to call the new Rectangle's area method to compute the area of the new rectangle.

Some methods, like area, return a value. For methods that return a value, you can use the method call in expressions. You can assign the return value to a variable, use it to make decisions, or control a loop. This code assigns the value returned by area to a variable:

int areaOfRectangle = new Rectangle(100, 50).area();
Remember, invoking a method on a particular object is the same as sending a message to that object. In this case, the object is the rectangle called rect. You will probably get a different response if you send the same message to bob.


Previous | Next | Trail Map | Learning the Java Language | Objects and Classes in Java