Start of Tutorial > Start of Trail > Start of Lesson | Search |
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:
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.
- Manipulate or inspect its variables.
- Call its methods.
Both the
Point
class and theRectangle
class allow free access to their member variables. You cannot put aPoint
object in an inconsistent state by settingx
ory
directly, and you cannot put aRectangle
object in an inconsistent state by settingwidth
,height
, ororigin
.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 aRectangle
object represents a rectangular object in a drawing program and the user just dragged it to a new location. You need to update theRectangle
object's point of origin. TheRectangle
class provides two equivalent ways of doing this:
- Manipulate the object's
origin
variable directly.- Call the
move
method.Rectangle
'sorigin
member is accessible to other classes (it's declared public), so you can assume that manipulating aRectangle
'sorigin
member directly is safe.
This section focuses on how to move theRectangle
by modifying itsorigin
variable directly. The next section shows you how to move the rectangle by calling themove
method.Assume you created a rectangle named
rect
as described in the previous section. To moverect
to a new location, you would write:This statement moves the rectangle by setting its point of origin to a new position.rect.origin = new Point(15, 37);rect.origin
is the name ofrect
'sorigin
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 torect.origin
.The
Rectangle
class has two other variables--width
andheight
-- 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 thearea
method):In general, to refer to an object's variables, append the name of the variable to an object reference with an intervening period (.):area = rect.height * rect.width;The first part of the variable's name,objectReference.variableobjectReference
, must be a reference to an object. You can use an object name here just as was done in the previous examples withrect
. You also can use any expression that returns an object reference. Recall that thenew
operator returns a reference to an object. So you could use the value returned fromnew
to access a new object's variables:This statement creates a newheight = new Rectangle().height;Rectangle
object and immediately gets its height. Effectively, the statement calculates the default height of aRectangle
. Note that after this statement has been executed, the program no longer has a reference to theRectangle
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 haveorigin
,width
, andheight
variables that they got from theRectangle
class. When you access a variable through an object reference, you reference that particular object's variables. Suppose thatbob
is also a rectangle in your drawing program and it has a different height and width thanrect
. The following instruction calculates the area of the rectangle namedbob
, which differs from the previous instruction that calculated the area ofrect
:area = bob.height * bob.width;
To moverect
to a new location using itsmove
method, you write this:This Java statement callsrect.move(15, 37);rect
'smove
method with two integer parameters, 15 and 37. It moves therect
object because therect
method assigns new values toorigin.x
andorigin.y
and is equivalent to the assignment statement used previously: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.rect.origin = new Point(15, 37);As stated previously in this lesson,objectReference.methodName(argumentList); or objectReference.methodName();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. Thenew
operator returns an object reference, so you can use the value returned fromnew
to call a new object's methods:The expressionnew Rectangle(100, 50).area()new Rectangle(100, 50)
returns an object reference that refers to aRectangle
object. As shown, you can use the dot notation to call the newRectangle
'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: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 calledint areaOfRectangle = new Rectangle(100, 50).area();rect
. You will probably get a different response if you send the same message tobob
.
Start of Tutorial > Start of Trail > Start of Lesson | Search |