Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
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:
- Manipulate or inspect its variables.
- Call its methods.
TheCreateObjectDemo
program creates a rectangle namedrect_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:This statement moves the rectangle by setting its point of origin to a new position.rect_two.origin = origin_one;rect_two.origin
is the name ofrect_two
'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_two.origin
.The
Rectangle
class has two other variables--width
andheight
-- that are accessible to objects outside of the class. The program displays them with this code:In general, to refer to an object's variables, append the name of the variable to an object reference with an intervening period (.):System.out.println("Width of rect_one: " + rect_one.width); System.out.println("Height of rect_one: " + rect_one.height);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
'sarea
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 returned by the constructor.int areaOfRectangle = new Rectangle(100, 50).area();
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 theRectangle
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 thewidth
and theheight
. 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 theRectangle
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 |