Contoh di sini terdiri dari dua kelas yaitu "Time1" dan "Time1Test". Kelas "Time1" menunjukkan time dari hari. Kelas "Time1Test" merupakan sebuah aplikasi kelas dimana main method menciptakan suatu objek dari kelas "Time 1" dan memanggil method-nya. Kelas-kelas ini harus dideklarasikan menjadi file terpisah karena keduanya merupakan public classes.
Berikut source code dari kelas "Time1":
1: /**
2: * Program Kelas Time1
3: * Deklarasi kelas Time1 mengelola waktu dalam format 24 jam
4: * Nama file : Time1.java
5: */
6:
7: public class Time1
8: {
9: private int hour;
10: private int minute;
11: private int second;
12: public void setTime(int h, int m, int s)
13: {
14: if ( ( h>=0 && h<24 ) && ( m>=0 && m<60 ) &&
15: ( s>= 0 && s<60 ) )
16: {
17: hour = h;
18: minute = m;
19: second = s;
20: }
21: else
22: throw new IllegalArgumentException(
23: "hour, minute and/or second was out of range" );
24: }
25: public String toUniversalString()
26: {
27: return String.format( "%02d:%02d:%02d", hour, minute, second );
28: }
29: public String toString()
30: {
31: return String.format( "%d:%02d:%02d %s", (( hour == 0 || hour == 12) ? 12
32: : hour % 12 ), minute , second, ( hour < 12 ? "AM" : "PM" ));
33: }
34: }
Berikut source code dari kelas "Time1Test":
1: /**
2: * Program Kelas Time1Test
3: * Objek Time1 yang digunakan dalam aplikasi
4: * Nama file : Time1Test.java
5: */
6:
7: public class Time1Test
8: {
9: public static void main( String[] args )
10: {
11: Time1 time = new Time1();
12: System.out.print("The initial universal time is: ");
13: System.out.println(time.toUniversalString());
14: System.out.print("The initial standard time is: ");
15: System.out.println(time.toString());
16: System.out.println();
17: time.setTime( 13, 27, 6 );
18: System.out.print("Universal time after setTime is: ");
19: System.out.println( time.toUniversalString() );
20: System.out.print("Standard time after setTime is: " );
21: System.out.println( time.toString() );
22: System.out.println();
23: try
24: {
25: time.setTime(99, 99, 99);
26: }
27: catch (IllegalArgumentException e )
28: {
29: System.out.printf("Exception: %s\n\n", e.getMessage());
30: }
31: System.out.println("After attempting invalid settings:");
32: System.out.print("Universal time: ");
33: System.out.println(time.toUniversalString());
34: System.out.print("Standar time: ");
35: System.out.println(time.toString());
36: }
37: }
Jika kedua kelas itu sudah dibuat, jika di-compile akan mendapatkan output seperti ini:
B. CONTROLLING ACCESS TO MEMBERS
Di sini akan dijelaskan bahwa anggota kelas private tidak dapat diakses di luar kelas. Ketika program ini dijalankan, compiler akan menunjukkan error messages yang menunjukkan bahwa private members tidak bisa diakses. Program ini memerlukan source code dari Time1.
Berikut souce code dari kelas "MemberAccessTest":
1: /**
2: * Program Kelas MemberAccessTest
3: * Anggota Private dari kelas Time1 tidak dapat diakses
4: * Nama file : MemberAccessTest.java
5: */
6:
7: public class MemberAccessTest
8: {
9: public static void main(String[]args)
10: {
11: Time1 time = new Time1();
12: time.hour = 7;
13: time.minute = 15;
14: time.second = 30;
15: }
16: }
17:
Jika source code di atas dijalankan, akan terjadi seperti ini:
C. REFERRING TO THE CURRENT OBJECT'S MEMBERS WITH THE this REFERENCE
Setiap objek bisa mengakses reference ke dirinya sendiri menggunakan keyword "this".
Berikut source code dari "ThisTest":
1: /**
2: * Program Kelas ThisTest
3: * Digunakan secara implisit dan eksplisit untuk merujuk ke anggota dari suatu objek
4: * Nama file : ThisTest.java
5: */
6: public class ThisTest
7: {
8: public static void main (String []args)
9: {
10: SimpleTime time = new SimpleTime(15, 30, 19);
11: System.out.println(time.buildString());
12: }
13: }
14:
15: class SimpleTime
16: {
17: private int hour;
18: private int minute;
19: private int second;
20:
21: public SimpleTime(int hour, int minute, int second)
22: {
23: this.hour = hour;
24: this.minute = minute;
25: this.second = second;
26: }
27:
28: public String buildString()
29: {
30: return String.format("%24s: %s\n%24s: %s",
31: "this.toUniversalString()", this.toUniversalString(),
32: "toUniversalString()", toUniversalString());
33: }
34:
35: public String toUniversalString()
36: {
37: return String.format("%02d:%02d:%02d",
38: this.hour, this.minute, this.second);
39: }
40: }
Jika source code di atas dijalankan, akan mempunyai output seperti ini:
D. TIME CLASS CASE STUDY: OVERLOADED CONSTRUCTORS
Seperti yang kita tahu, kita dapat mendeklarasikan constructor kita sendiri untuk menentukan bagaimana objek dari suatu kelas harus dinisialisasi. Selanjutnya, akan dijelaskan suatu kelas yg memiliki beberapa overloaded constructor yang memungkinkan objek dari suatu kelas dapat diinisialisasikan secara berbeda. Untuk melakukan overload constructor, sediakan beberapa deklarasi constructor dengan signature yang berbeda.
Berikut souce code dari kelas "Time2":
1: /**
2: * Program Kelas Time2
3: * Kelas Time2 yang dideklarasikan dengan overloaded constructors.
4: * Nama file : Time2.java
5: */
6:
7: public class Time2
8: {
9: private int hour;
10: private int minute;
11: private int second;
12:
13: public Time2()
14: {
15: this(0, 0, 0);
16: }
17:
18: public Time2(int h)
19: {
20: this(h,0,0);
21: }
22:
23: public Time2(int h, int m)
24: {
25: this(h,m,0);
26: }
27:
28: public Time2(int h, int m, int s)
29: {
30: setTime(h,m,s);
31: }
32:
33: public Time2(Time2 time)
34: {
35: this(time.getHour(), time.getMinute(), time.getSecond());
36: }
37:
38: public void setTime (int h, int m, int s)
39: {
40: setHour(h);
41: setMinute(m);
42: setSecond(s);
43: }
44:
45: public void setHour(int h)
46: {
47: if(h>=0 && h<24)
48: hour = h;
49: else
50: throw new IllegalArgumentException("hour must be 0-23");
51: }
52:
53: public void setMinute(int m)
54: {
55: if(m>=0 && m<60)
56: minute = m;
57: else
58: throw new IllegalArgumentException("minute must be 0-59");
59: }
60:
61: public void setSecond(int s)
62: {
63: if(s>= 0 && s<60)
64: second = ((s>=0 && s<60)? s : 0);
65: else
66: throw new IllegalArgumentException("second must be 0-59");
67: }
68:
69: public int getHour()
70: {
71: return hour;
72: }
73:
74: public int getMinute()
75: {
76: return minute;
77: }
78:
79: public int getSecond()
80: {
81: return second;
82: }
83:
84: public String toUniversalString()
85: {
86: return String.format(
87: "%02d:%02d:%02d", getHour(), getMinute(), getSecond() );
88: }
89:
90: public String toString()
91: {
92: return String.format("%d:%02d:%02d %s",
93: ((getHour() == 0 || getHour()==12) ? 12 : getHour () %12),
94: getMinute(), getSecond(), (getHour()<12 ? "AM" : "PM"));
95: }
96: }
97:
Berikut souce code dari kelas "Time2Test" agar "Time2" dapat dicompile:
1: /**
2: * Program Kelas Time2Test
3: * Overloaded constructors yang digunakan untuk menginisialisasi objek Time2
4: * Nama file : Time2Test.java
5: */
6:
7: public class Time2Test
8: {
9: public static void main(String[]args)
10: {
11: Time2 t1 = new Time2();
12: Time2 t2 = new Time2(2);
13: Time2 t3 = new Time2(21, 34);
14: Time2 t4 = new Time2(12, 25, 42);
15: Time2 t5 = new Time2(t4);
16:
17: System.out.println ("Constructed with:");
18: System.out.println ("t1: all arguments defaulted");
19: System.out.printf (" %s\n", t1.toUniversalString());
20: System.out.printf (" %s\n", t1.toString());
21:
22: System.out.println ("t2: hour specified; minute and second defaulted");
23: System.out.printf (" %s\n", t2.toUniversalString());
24: System.out.printf (" %s\n", t2.toString());
25:
26: System.out.println ("t3: hour and minute specified; second defaulted");
27: System.out.printf (" %s\n", t3.toUniversalString());
28: System.out.printf (" %s\n", t3.toString());
29:
30: System.out.println ("t4: hour, minute and second specified");
31: System.out.printf (" %s\n", t4.toUniversalString());
32: System.out.printf (" %s\n", t4.toString());
33:
34: System.out.println ("t5: Time2 object t4 specified");
35: System.out.printf (" %s\n", t5.toUniversalString());
36: System.out.printf (" %s\n", t5.toString());
37:
38: try
39: {
40: Time2 t6 = new Time2(27, 74, 99);
41: }
42: catch (IllegalArgumentException e)
43: {
44: System.out.printf( "\nException while initializing t6: %s\n", e.getMessage());
45: }
46: }
47: }
Jika kedua kelas itu sudah dibuat, jika di-compile akan mendapatkan output seperti ini: