You know that you can refer to an
instance-variable value directly by using the field-selection operator.
For example, suppose that you have defined the Attraction
class with a
minutes
instance variable that records the duration of an
entertainment attraction
in minutes. You can refer to the value of that instance variable in a
particular attraction, x
:
x.minutes
Alternatively, you can refer to an instance-variable value indirectly by
defining an instance method that returns the instance-variable value. In
the following Attraction
class definition, for example, an instance
method named getMinutes
returns the value of the minutes
instance variable:
public class Attraction { // First, define instance variable: public int minutes; // Define zero-parameter constructor: public Attraction () {minutes = 75;} // Define one-parameter constructor: public Attraction (int m) {minutes = m;} // Define getter: public int getMinutes () { return minutes; } }
With getMinutes
defined, you have another way to refer to the
value of the minutes
instance variable of a particular attraction, x
:
x.getMinutes()