Home Segments Top Top Previous Next

281: Mainline

If you decide that an assignment of the minutes instance variable never changes once the Attraction instance is constructed, you can reflect that decision by removing the setter, setMinutes. Then, only the constructor can assign a value to the minutes instance variable:

public class Attraction { 
 // 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;} 
 // No setter defined here 
 // Define private variable:     
 private int minutes;            
} 

Because getMinutes is defined publicly in the Attraction class, the value of the minutes instance variable is accessible via getMinutes everywhere. Once construction is complete, however, the minutes instance variable cannot be written everywhere, because there is no publicly defined setter.