Referencing an Object’s Variables

Assume we create a rectangle named rect as described in the previous section. To move rect to a new location,
we would write:
rect.origin = new Point(15, 30);

This statement moves the rectangle by setting its point of origin to a new position. rect.origin is the name of rect’s origin variable.

The Rectangle class has two other variables– width and height– that are accessible to objects outside of the class. We can use the same notation to access them and calculate the rectangle’s area using this statement
area = rect.height * rect.width;;

In general, to refer to an object’s variables, append the name of the variable to an object reference with an intervening period (.):

The first part of the variable’s name, objectReference, must be a reference to an object.

Scroll to Top