You may use a simple name for a field within its own class. For example, we can add a statement
within the Rectangle
class that prints the width
and height
:
System.out.println("Width and height are: " + width + ", " + height);
width
and height
are simple names.
Code that is outside the object's class must use an object reference or expression, followed by the dot (.) operator, followed by a simple field name, as in:
objectReference.fieldName
For example, the code in the CreateObjectDemo class is outside the code for the Rectangle class. So to refer to the origin, width, and height fields within the Rectangle object named rectOne, the CreateObjectDemo class must use the names rectOne.origin, rectOne.width, and rectOne.height, respectively. The program uses two of these names to display the width and the height of rectOne:
System.out.println("Width of rectOne: " + rectOne.width); System.out.println("Height of rectOne: " + rectOne.height);
Later, the program uses similar code to display information about rectTwo. Objects of the same type have their own copy of the same instance fields. Thus, each Rectangle object has fields named origin, width, and height. When you access an instance field through an object reference, you reference that particular object's field. The two objects rectOne and rectTwo in the CreateObjectDemo program have different origin, width, and height fields.
To access a field, you can use a named reference to an object, as in the previous examples, or you 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 fields:
int height = new Rectangle().height;
objectReference.methodName(argumentList); or objectReference.methodName();
System.out.println("Area of rectOne: " + rectOne.getArea()); ... rectTwo.move(40, 72);
getArea()
method and displays
the results. The second line moves rectTwo because the
move() method assigns new values to the object's origin.x
and origin.y.
As with instance fields, objectReference must be a reference to an object. You can use a variable name, 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 invoke a new object's methods:
new Rectangle(100, 50).getArea()
Some methods, such as getArea(), return a value.
For methods that return a value, you can use the method invocation
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 getArea() to the variable areaOfRectangle
:
int areaOfRectangle = new Rectangle(100, 50).getArea();
An object is eligible for garbage collection when there are no more references to that object. References that are held in a variable are usually dropped when the variable goes out of scope. Or, you can explicitly drop an object reference by setting the variable to the special value null. Remember that a program can have multiple references to the same object; all references to an object must be dropped before the object is eligible for garbage collection.
The Java runtime environment has a garbage collector that periodically frees the memory used by objects that are no longer referenced. The garbage collector does its job automatically when it determines that the time is right.