PROGRAMMING #8 : Java Programming for Programmer - Classes and Objects (PART 3) | SUB [ENG-BAHASA]

in #programming7 years ago

RESOURCES TO LEARN JAVA

In this article is a continuation of the previous article Java Programming for Programmer - Classes and Objects (PART 2). Still in the same topic, which I will discuss about classes and objects. Immediately on the following points.

Controlling Access to Members

Public and private access modifiers used to control access to variables and member of a class. As explained, the main purpose of public methods is to provide users with the class of services that the class provides. Users do not have to worry about how the class completes its task.

Listing 1.3 demonstrates that private members of a class are not available to users outside the class. Lines 9-11 try to access directly instance variables hours, minutes, and seconds of the Time1 object, time. When this program is compiled, the compiler will generate an error message stating that the three private variables can not be accessed. This program uses the Time1 class In code 1.1.

Code 1.3

Referring Current Object Member to Reference This

Each object can access a reference (pointing) to itself using this keyword (also called this reference). When a non-static method is called on a particular object, the method body implicitly uses this keyword to refer to instance variables and other methods (property of the object). This allows the class code to determine which objects to be manipulated. As you will see in code 1.4, you can use this keyword explicitly in the body of a non-static method.

It will be implicitly demonstrated and explicitly the usefulness of this reference (code 1.4). An example of this is the first example, where it declared two classes in a single file, UjiThis class declared on lines 4-11 and WaktuSederhana class on the line 14-47. This was done to demonstrate that when you compile a .java file that contains more than one class, compiler will generate a separate class file with the extension .class for each class is compiled. Note also that the only class 4 code UjiThis declared public. A source code (.java) may only contain one public class, if more damage to compilation will occur. Non-public classes can only be used by other classes of fellow packages. So, in this example, the class WaktuSederhana can only be used by UjiThis class.

Code 1.4

WaktuSederhana class (lines 14-47) declares two private instance variable, ie hours, minutes, and seconds (lines 16-18). The constructor (lines 23-28) accepts three arguments to initialize the Simple Time object. Used parameter names for the constructor (line 23) which is identical to the instance variable instance names (lines 16-18). This practice is not recommended, but made here to hide instance variables so they can be simply illustrated a case in which in which the explicit use of this reference is needed. If a method loads a local variable that has the same name as the data field. In this case, local variables oscillate or hide the data fields within the method scopes. However, this method can be used to refer to the reference data fields shaded or hidden, as shown on the left side of the assignment on line 25-27 for WaktuSederhana instance variables.

The constructString method (line 31-36) returns a String created through a statement that uses this reference explicitly and implicitly. Line 34 used explicitly to call the same method. Both lines do the same job. You generally do not use this explicitly to refer to other methods in the current object. Also, on line 45 the toUniversalString method uses explicitly reference this to access each instance variable. This is not necessary, because the method does not have local variables that overshadow the instance variable of the class.

The construct method of String (lines 31-36) reverses a String created through a statement that uses this reference explicitly and implicitly. Line 34 uses it explicitly to call the toUniversalString method. Line 35 uses it implicitly to call the same method. Both lines do the same job. You generally do not use this explicitly to refer to other methods in the current object. Also, on line 45 the toUniversalString method uses explicitly reference this to access each instance variable. This is not necessary, because the method does not have local variables that overshadow the instance variable of the class.

To be continued...


[BAHASA]


Pada artikel ini merupakan lanjutan dari artikel sebelumnya yaitu PROGRAMMING #7 : Java Programming for Programmer - Classes and Objects (PART 2). Masih dalam topik yang sama, dimana saya akan membahas mengenai kelas dan objek. Langsung saja pada pokok pembahasan berikut ini.

Mengendalikan Akses ke Anggota

Pemodifikasi akses public dan private digunakan untuk mengendalikan akses terhadap variabel dan anggota suatu kelas. Seperti yang telah dijelaskan, tujuan utama dari metode public adalah untuk menyediakan kepada pengguna kelas layanan yang disediakan kelas. Pengguna tidak perlu khawatir bagaimana kelas menuntaskan tugasnya. Karena alasan ini, variabel private tidak tersedia (detail implementasi) tidak tersedia bagi pengguna atau klien kelas.

Kode 1.3 mendemonstrasikan bahwa anggota-anggota private suatu kelas tidak tersedia bagi pengguna diluar kelas tersebut. Baris 9-11 mencoba untuk mengakses secara langsung variabel contoh jam, menit, dan detik dari objek Waktu1, waktu. Ketika program ini dikompilasi, penyusun akan membangkitkan pesan kesalahan yang menyatakan bahwa ketiga variabel private tersebut tidak dapat diakses. Program ini menggunakan kelas Waktu1 pada kode 1.1

Kode 1.3

Merujuk Anggota Objek Sekarang dengan Referensi This

Setiap objek dapat mengakses suatu referensi (yang menunjuk) kepada dirinya sendiri menggunakan kata kunci this (disebut pula dengan referensi this). Ketika suatu metode non-statik dipanggil pada objek tertentu, tubuh metode tersebut secara implisit menggunakan kata kunci this untuk merujuk kepada variabel-variabel contoh dan metode-metode lain (milik dari objek tersebut). Hal ini memungkinkan kode kelas untuk mengetahui objek mana yang harus di manipulasi. Seperti yang akan anda lihat pada kode 1.4, anda bisa menggunakan kata kunci this secara eksplisit di dalam tubuh suatu metode non-statik.

Akan di demonstrasikan secara implisit dan secara eksplisit kegunaan dari referensi this (kode 1.4). Contoh ini merupakan contoh pertama, dimana di dalamnya dideklarasikan dua kelas di dalam satu file, kelas UjiThis dideklarasikan pada baris 4-11 dan kelas WaktuSederhana pada baris 14-47. Hal ini dilakukan untuk mendemonstrasikan bahwa ketika anda mengompilasi suatu file .java yang memuat lebih dari satu kelas, penyusun akan menghasilkan file kelas terpisah dengan ekstensi .class untuk setiap kelas yang dikompilasi. Perhatikan pula bahwa pada kode 4 hanya kelas UjiThis yang dideklarasikan public. Suatu kode sumber (.java) hanya boleh memuat satu kelas public, jika lebih kerusakan kompilasi akan terjadi. Kelas-kelas non-public hanya bisa digunakan oleh kelas-kelas lain sesama paket. Jadi, pada contoh ini, kelas WaktuSederhana hanya bisa digunakan oleh kelas UjiThis.

Kode 1.4

Kelas WaktuSederhana (baris 14-47) mendeklarasikan tiga variabel contoh private, yakni jam, menit, dan detik (baris 16-18). Konstruktor (baris 23-28) menerima tiga argumen int untuk menginisialisasi objek WaktuSederhana. Digunakan nama-nama parameter untuk konstruktor (baris 23) yang identik dengan nama-nama variabel contoh kelas (baris 16-18). Praktik ini tidak direkomendasikan, tetapi dilakukan di sini untuk menyembunyikan variabel-variabel contoh tersebut agar dapat di ilustrasikan suatu kasus dimana di dalamnya penggunaan eksplisit dari referensi this dibutuhkan. Jika suatu metode memuat variabel lokal yang memiliki nama sama dengan bidang data. Pada kasus ini, variabel lokal membayangi atau menyembunyikan bidang data di dalam skop metode. Tetapi, metode dapat menggunakan referensi this untuk merujuk pada bidang data yang terbayangi atau tersembunyi, seperti ditunjukkan pada sisi sebelah kiri penugasan pada baris 25-27 untuk variabel-variabel contoh WaktuSederhana.

Metode bangunString (baris 31-36) mengembalikan suatu String yang diciptakan melalui suatu pernyataan yang menggunakan referensi this secara eksplisit dan implisit. Baris 34 menggunakannya secara eksplisit untuk memanggil metode yang sama. Kedua baris tersebut melakukan pekerjaan yang sama. Anda secara umum tidak mnggunakan this secara eksplisit untuk merujuk kepada metode-metode lain di dalam objek sekarang. Juga, pada baris 45 metode toUniversalString menggunakan secara eksplisit referensi this untuk mengakses setiap variabel contoh. Hal ini tidak diperlukan, karena metode tidak memiliki variabel lokal yang membayangi variabel contoh kelas.

Metode bangunString (baris 31-36) mengembalikaan suatu String yang diciptakan melalui suatu pernyataan yang menggunakan referensi this secara eksplisit dan implisit. Baris 34 menggunakannya secara eksplisit untuk memanggil metode toUniversalString. Baris 35 menggunakannya secara implisit untuk memanggil metode yang sama. Kedua baris tersebut melakukan pekerjaan yang sama. Anda secara umum tidak menggunakan this secara eksplisit untuk merujuk kepada metode-metode lain di dalam objek sekarang. Juga, pada baris 45 metode toUniversalString menggunakan secara eksplisit referensi this untuk mengakses setiap variabel contoh. Hal ini tidak diperlukan, karena metode tidak memiliki variabel lokal yang membayangi variabel contoh kelas.

Bersambung...


FOLLOW ME AT

THANK'S FOR READING THIS POST, AND THANK'S FOR YOUR SUPPORT. IF YOU LIKE IT, UP VOTE AND KEEP RESTEEM.

LOOK FORWARD TO THE NEXT POST >>>


INDONESIAN STEEMIT COMMUNITY

Sort:  

Nice post....

Terima kasih banyak

Sudah di upvote ya mas..
Salam kenal

Terima kasih banyak mas, salam kenal kembali hehe @rikiandria

Sama sama bg...
Pkok nya any time bg

Coin Marketplace

STEEM 0.17
TRX 0.13
JST 0.029
BTC 56583.42
ETH 2984.52
USDT 1.00
SBD 2.15