The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Trail: Learning the Java Language
Lesson: Object and Data Basics

## 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.

### Referencing an Object's Variables

> The `CreateObjectDemo` program creates a rectangle named `rect_two`. The constructor used to create that rectangle initializes the rectangle's origin to 0, 0. Later the program changes the rectangle's origin with this statement:

> >     rect_two.origin = origin_one;

> This statement moves the rectangle by setting its point of origin to a new position. `rect_two.origin` is the name of `rect_two`'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_two.origin`.

> The `Rectangle` class has two other variables-- `width` and `height`-- that are accessible to objects outside of the class. The program displays them with this code:

> >     System.out.println("Width of rect_one: " + rect_one.width);
> >     System.out.println("Height of rect_one: " + rect_one.height);

> 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 returned by the constructor.

### A Word About Variable Access

> Ideal object-oriented programming discourages the direct manipulation of an object's variables because it would be possible to set the variables to values that don't make sense. For example, consider the `Rectangle` class from the previous section. Using that class, you can create a rectangle whose width and height are negative, which, for some applications, doesn't make sense.

> Instead of allowing direct manipulation of an its variables, an ideal class provides methods through which you can inspect or change its variables. These methods ensure that the values of the variables make sense for objects of that type. So, the `Rectangle` class would provide methods for setting and getting the `width` and the `height`. The methods for setting the variables would report an error, if the caller tried to set the width or height to a negative number.

> However, in practical situations, it sometimes makes sense to allow direct access to an object's variables. For example, both the `Point` class and the `Rectangle` class allow free access to their member variables. This keeps these classes small and simple. Also, it keeps them generally useful. Some applications might allow rectangles with negative widths and heights.

> Java provides an access control mechanism whereby classes can determine which other classes can have direct access to its variables and methods. A class should protect variables against direct manipulation by other objects if those manipulations could result in values that don't make sense objects of that type. 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.

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form