Java Objects |
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:
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
- call the
move()
method- manipulate the object's x, y variables directly
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'smove()
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.
Back to the drawing application, if the user moved the rectangle namedobject.method(argumentList); or object.method();rect
to a new location, say 244, 47, the call torect
'smove()
method would look like this:This Java statement callsrect.move(244, 47);rect
'smove()
method with two integer parameters, 244 and 47. This statement has the effect of moving therect
object to the x, y location 244, 47. If you wanted to move a different rectangle, namedbob
for example, to a new location you would write: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 namedbob.move(244, 47);rect
amove
message,rect
moves to the new location. When you send the object namedbob
amove
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'sinside()
method does. Theinside()
method takes an x, y coordinate and returns true if that point lies within the rectangle. So you could use theinside()
method to determine if some point, say the current mouse location, was inside the rectangle and do something special if it were.Remember that the method call is like a message to the object named. In this case, the object named is the Rectangle namedif (rect.inside(mouse.x, mouse.y)) { // mouse is in the rectangle . . . } else { // mouse is outside of the rectangle . . . }rect
. Thus,is askingrect.inside(mouse.x, mouse.y)rect
if the mouse cursor location represented bymouse.x
andmouse.y
is contained within it. You would likely get a different response if you sent the same message tobob
.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:The expressionnew Rectangle(0, 0, 100, 50).equals(anotherRect)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'sequals()
method to determine if the new rectangle is equal to the one specified inequals()
'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).You can use this notation to either view or modify an object's variables. For example, we can changeobject.variablerect
's height and width using the following two statements:Or you can calculate the rectangle's area using these statementsrect.height = 15; rect.width = 37;Similar to method calls, when you reference a variable through an object, you are referencing a particular instances variables. Ifarea = rect.height * rect.width;bob
andrect
are both rectangles of different height and widths, this instructioncalculates the area of the rectangle namedarea = rect.height * rect.width;rect
and this instructioncalculates the area of the rectangle namedarea = bob.height * bob.width;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;
Table of Contents |