|
|
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
Pointclass and theRectangleclass allow free access to their member variables. You cannot put aPointobject in an inconsistent state by settingxorydirectly, and you cannot put aRectangleobject 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
Rectangleobject. Suppose aRectangleobject represents a rectangular object in a drawing program and the user just dragged it to a new location. You need to update theRectangleobject's point of origin. TheRectangleclass provides two equivalent ways of doing this:
- Manipulate the object's
originvariable directly.- Call the
movemethod.Rectangle'soriginmember is accessible to other classes (it's declared public), so you can assume that manipulating aRectangle'soriginmember directly is safe.
This section focuses on how to move theRectangleby modifying itsoriginvariable directly. The next section shows you how to move the rectangle by calling themovemethod.Assume you created a rectangle named
rectas described in the previous section. To moverectto 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.originis the name ofrect'soriginvariable. 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
Rectangleclass has two other variables--widthandheight-- 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 theareamethod):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 thenewoperator returns a reference to an object. So you could use the value returned fromnewto access a new object's variables:This statement creates a newheight = new Rectangle().height;Rectangleobject 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 theRectanglethat 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
Rectangleobjects haveorigin,width, andheightvariables that they got from theRectangleclass. When you access a variable through an object reference, you reference that particular object's variables. Suppose thatbobis 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 moverectto a new location using itsmovemethod, you write this:This Java statement callsrect.move(15, 37);rect'smovemethod with two integer parameters, 15 and 37. It moves therectobject because therectmethod assigns new values toorigin.xandorigin.yand 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();objectReferencemust 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. Thenewoperator returns an object reference, so you can use the value returned fromnewto call a new object's methods:The expressionnew Rectangle(100, 50).area()new Rectangle(100, 50)returns an object reference that refers to aRectangleobject. 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 |