... accessible outside the class. Lines 7–9 attempt to access the private instance variables hour, minute and second of the Time1 object time. When this program is compiled, the compiler generates error messages that these private members are not accessible. This program assumes that the Time1 class from Fig. 8.1 is used.

Fig. 8.3

 1   // Fig. 8.3: MemberAccessTest.java
 2   // Private members of class Time1 are not accessible.
 3   public class MemberAccessTest {
 4      public static void main(String[] args) {
 5         Time1 time = new Time1(); // create and initialize Time1 object
 6
 7         time.hour = 7; // error: hour has private access in Time1
 8         time.minute = 15; // error: minute has private access in Time1
 9         time.second = 30; // error: second has private access in Time1 ...

Get Java How to Program, Early Objects, 11th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.