MODUL 2
ACCESS DETERMINER : PRIVATE AND
PUBLIC
A. OBJECTIVE
To Understand the comparison
between private and public access.
B. Basic
Teory
Related to the availability an instant variable accessed from out
of the class, Java serve an access determiner:
1.
Public mean that accessing of an
instant variable or method can be done from out of the class. 2. Private mean that accessing of an instant variable or method only can be done in the class, can not accessed from out of the class.
·
Laptop
·
NetBeans software
·
Java Development Kit (JDK)
D.
Steps
First Experiment
1.
Make a java class named “Mahasiswa.java”,
write down the program code below:
/**
*
* @author DESY
*/
public class Mahasiswa {
//variable instan
String nama;
//methode
void
isiNama(String nama){
this.nama=nama;
}
//methode
String tampilkanNama(){
return this.nama;
}
}
2.
Compile that code
3.
Next, make a new class named “PenentuAkses.java”,
look likes below. Change the name with your name.
/**
*
* @author DESY
*/
public class PenentuAkses {
public static
void main (String []args){
Mahasiswa saya = new Mahasiswa();
//mengisi variable instan
saya.isiNama("Desy Puspitasari");
//menampilkan nama melaui variable
System.out.println(saya.nama);
//menampilkan nama melalui pemanggilan
methode
System.out.println(saya.tampilkanNama());
}
}
4.
Compile that code and then analize
the result.
Excercices
1.
Change the access determiner from
name variable to private access, the step is adding private keyword below
private String
nama;
2.
Next, save with same name then
compile.
3.
Then, compile file “PenentuAkses.java”
4.
Take an attention of the result and
compare with the result before variable access changed to private
Result:
public class Mahasiswa {
//variable instan
private String nama;
//methode
void
isiNama(String nama){
this.nama=nama;
}
//methode
String tampilkanNama(){
return this.nama;
}
}

2nd Experiment
1.
Make a new class named “Lingkaran.java”
and write down program code look likes below
/**
*
* @author DESY
*/
public class Lingkaran {
private
double radius;
//methode
void
isiJariJari(double radius){
this.radius =
radius;
}
private
double ambilPhi(){
return 3.14;
}
public
double hitungKeliling(){
return 2*ambilPhi()*radius;
}
}
2.
Compile that code
3.
Next, make a new class named “PenentuAksesMethod.java”
and write down the code below
/**
*
* @author DESY
*/
public class PenentuAksesMethod {
public
static void main (String []args){
Lingkaran bulatan = new Lingkaran();
//mengisi jari-jari
lingkaran
bulatan.isiJariJari(10);
//menampilkan keliling
lingkaran
System.out.println("Keliling = "+bulatan.hitungKeliling());
//Menampilkan nilai Phi
System.out.println("nilai Phi = "+bulatan.ambilPhi());
}
}
4.
Compile that code
5.
Analize the result and explain
Exercises
1.
Change the access determiner from
ambilPhi() method to public access, the step is by adding public keyword look
likes below
public double ambilPhi();
2.
Next, save the file with same name
as before and recompile.
3.
Then compile file “PenentuAksesMethod.java”
4.
Take an attention of the result and
compare with the result before the variable access changed to public.
/**
*
* @author DESY
*/
public class Lingkaran {
private
double radius;
//methode
void
isiJariJari(double radius){
this.radius =
radius;
}
public
double ambilPhi(){
return 3.14;
}
public
double hitungKeliling(){
return 2*ambilPhi()*radius;
}
}

Tidak ada komentar:
Posting Komentar