// Fig. 8.1: Time1.java
// Time1 class declaration maintains the time in 24-hour format.
public class Time1
{
private int hour; // 0 ~ 23
private int minute; // 0 ~ 59
private int second; // 0 ~ 59
// set a new time value using universal time: throw an
// exception if the hour, minute or second is invalid
public void setTime(int hour, int minute, int second)
{
if (hour < 0 || hour >= 24 || minute < 0 || minute >= 60 || second < 0 || second >= 60)
{
throw new IllegalArgumentException("hour, minute and/or second was out of range");
}
this.hour = hour;
this.minute = minute;
this.second = second;
}
// convert to String in universal-time format (HH:MM:SS)
public String toUniversalString()
{
return String.format("%02d:%02d:%02d", hour, minute, second);
}
// convert to String in standard-time format (H:MM:SS AM or PM)
public String toString()
{
return String.format("%d:%02d:%02d %s",
((hour == 0 || hour == 12) ? 12 : hour % 12),
minute, second, (hour < 12 ? "AM" : "PM"));
}
} // end class Time1
위 코드의 setTime 과 같은 방법의 경우, 인스턴스 변수 값을 설정하는 데 사용하기 전에 방법의 모든 인수를 검증하여 모든 인수가 유효한 경우에만 개체의 데이터가 수정되는지 확인합니다.
Chapter 3 에서 액세스 수정자로 선언된 방법은 비공개 방법이 선언된 클래스의 다른 방법으로만 호출할 수 있다는 점을 기억하세요. 이러한 방법은 일반적으로 클래스의 다른 방법의 작동을 지원하는 데 사용되기 때문에 utility methods 또는 helper methods 이라고도 합니다.
// Fig. 8.2: TimeTest.java
// Time1 object used in an app
public class TimeTest
{
public static void main(String[] args)
{
// create and initialize a Time1 object
Time1 time = new Time1(); // invokes Time1 constructor
// output string representations of the time
displayTime("After time object is created", time);
System.out.println();
// change time and output updated time
time.setTime(13, 27, 6);
displayTime("After calling setTime", time);
System.out.println();
// attempt to set time with invalid values
try
{
time.setTime(99, 99, 99); // all values out of range
}
catch(IllegalArgumentException e)
{
System.out.printf("Exception: %s%n%n", e.getMessage());
}
// display time after attempt to set invalid values
displayTime("After calling setTime with invalid values", time);
}
// displays a Time1 object in 24-hour and 12-hour formats
private static void displayTime(String header, Time1 t)
{
System.out.printf("%s%nUniversal time: %s%nStandard time: %s%n",
header, t.toUniversalString(), t.toString());
}
} // end Class TimeTest

Method setTime 그리고 Throwing Exceptions
잘못된 값의 경우, setTime 은 IllegalArgumentException 유형의 예외를 throw 합니다.
Class Time1 선언의 소프트웨어 엔지니어링
클래스는 프로그래밍을 단순화합니다.
클라이언트는 클래스의 public methods 만 사용할 수 있기 때문입니다.
이러한 방법은 일반적으로 구현 지향적이라기보다는 클라이언트 지향적입니다.
클라이언트는 클래스의 구현을 인식하지도, 관여하지도 않습니다.
클라이언트는 일반적으로 클래스가 하는 일에는 관심이 있지만 클래스가 하는 방식에는 관심이 없습니다.
인터페이스는 구현보다 덜 자주 변경됩니다.
구현이 변경되면 구현 종속 코드가 그에 따라 변경되어야 합니다.
구현을 숨기면 다른 프로그램 부분이 클래스 구현 세부 정보에 의존하게 될 가능성이 줄어듭니다.
Java SE 8 - 날짜/시간 API
// Fig. 8.3: MemberAccessTest.java
// Private members of class Time1 are not accessible.
public class MemberAccessTest
{
public static void main(String[] args)
{
Time1 time = new Time1(); // create and initialize Time1 object
time.hour = 7; // error: hour has private access in Time1
time.minute = 15; // error: hour has private access in Time1
time.second = 30; // error: hour has private access in Time1
}
} // and class MemberAccessTest

클래스의 멤버가 아닌 방법으로 해당 클래스의 private 멤버에 액세스하려고 하면 컴파일 오류가 발생합니다.