Class Instance Variable Dan Method
Class Instance Variable Dan Method
PTIIK - 2013
Objectives
Mampu mendeklarasikan class dan menggunakannya untuk
membuat object.
Mampu mendeklarasikan methods dalam class (tingkah laku
class).
Mampu mendeklarasikan instance variables dalam class
(atribut class).
Mampu memanggil method pada object untuk menjalankan
algoritma yang ada di dalamnya.
Memahami perbedaan antara instance variables dari class
dan local variables pada method.
Review Class, Object, Instance Variable
Parameter Method
Informasi tambahan yang dilewatkan melalui method
Ditambahakan pada saat memanggil method dengan
cara mengisi arguments
1 // Fig. 3.4: GradeBook.java
2 // Class declaration with a method that has a parameter.
3
4 public class GradeBook
5 {
6 // display a welcome message to the GradeBook user
7 public void displayMessage( String courseName )
8 {
9 System.out.printf( "Welcome to the grade book for\n%s!\n",
10 courseName ); Call printf method with
11 } // end method displayMessage courseName argument
12
13 } // end class GradeBook
1 // Fig. 3.5: GradeBookTest.java
2 // Create GradeBook object and pass a String to
3 // its displayMessage method.
4 import java.util.Scanner; // program uses Scanner
5
6 public class GradeBookTest
7 {
8 // main method begins program execution
9 public static void main( String args[] )
10 {
11 // create Scanner to obtain input from command window
12 Scanner input = new Scanner( System.in );
13
14 Call nextLine method to
// create a GradeBook object and assign it to myGradeBook
15 GradeBook myGradeBook = new GradeBook();
16
read a line of input
17 // prompt for and input course name
18 System.out.println( "Please enter the course name:" );
19 Call displayMessage with
String nameOfCourse = input.nextLine(); // read a line of text
20 System.out.println(); // outputs a blank line
21 an argument
22 // call myGradeBook's displayMessage method
23 // and pass nameOfCourse as an argument
24 myGradeBook.displayMessage( nameOfCourse );
25 } // end main
26
27 } // end class GradeBookTest
Set methods
Disebut juga sebagai mutator methods
Memberikan nilai pada instance variables
Seharusnya divalidasi terhadap nilai baru
• Dapat mengembalikan suatu nilai yang mengindikasikan data
yang salah
Get methods
Disebut juga sebagai accessor methods atau query
methods
Memperoleh / mendapatkan nilai dari instance variables
Dapat dikontrol format datanya
Catatan : Set and Get Methods
Predicate methods
Penggunaan lain dari accessor methods adalah untuk
menguji apakah suatu kondisi pada object : true atau
false dan mengembalikan hasilnya
Contoh : method isEmpty seperti linked list,
stack or queue
Access Modifiers public dan private
keyword Private
Sering digunakan untuk instance variables
private variables dan methods hanya bisa diakses
oleh methods di dalam class dimana method tersebut
dideklarasikan
Deklarasi instance variables private disebut
sebagai data hiding
Return type (Tipe Pengembalian)
Mengindikasikan item yang dikembalikan oleh method
Dideklarasikan pada header dari method
Precede every field and method declaration with an access modifier. As a rule of
thumb, instance variables should be declared private and methods should be
declared public. (We will see that it is appropriate to declare certain methods
private, if they will be accessed only by other methods of the class.)
1 // Fig. 3.7: GradeBook.java
2 // GradeBook class that contains a courseName instance variable
3 // and methods to set and get its value.
4 Instance variable
5 public class GradeBook
6 { courseName
7 private String courseName; // course name for this GradeBook
8
9 // method to set the course name
10 public void setCourseName( String name )
11 { set method for courseName
12 courseName = name; // store the course name
13 } // end method setCourseName
14
15 // method to retrieve the course name
16 public String getCourseName()
17 { get method for courseName
18 return courseName;
19 } // end method getCourseName
20
21 // display a welcome message to the GradeBook user
22 public void displayMessage()
23 {
24 // this statement calls getCourseName to get the
25 // name of the course this GradeBook represents
26 System.out.printf( "Welcome to the grade book for\n%s!\n",
27 getCourseName() ); Call get method
28 } // end method displayMessage
29
30 } // end class GradeBook
1 // Fig. 3.8: GradeBookTest.java
2 // Create and manipulate a GradeBook object.
3 import java.util.Scanner; // program uses Scanner
4
5 public class GradeBookTest
6 {
7 // main method begins program execution
8 public static void main( String args[] )
9 {
10 // create Scanner to obtain input from command window
11 Scanner input = new Scanner( System.in );
12
13 // create a GradeBook object and assign it to myGradeBook
14 GradeBook myGradeBook = new GradeBook();
15
16 // display initial value of courseName
17 System.out.printf( "Initial course name is: %s\n\n",
18 myGradeBook.getCourseName() );
19 Call get method for
courseName
20 // prompt for and read course name
21 System.out.println( "Please enter the course name:" );
22 String theName = input.nextLine(); // read a line of text
23 myGradeBook.setCourseName( theName ); // set the course name
24 System.out.println(); // outputs a blank line Call set method for
25 courseName
26 // display welcome message after specifying course name
27 myGradeBook.displayMessage();
28 } // end main Call displayMessage
29
30 } // end class GradeBookTest