Vous êtes sur la page 1sur 6

MODULE 2

ACCESS DETERMINATION:
PRIVATE AND PUBLIC

A. THE PURPOSE
Introduce to students the provided methods to access instant variable in java.
To get students understand public and private access determination related
on whether or not instan variable can be accessed from outside the class

B. THEORETICAL BASIS
In object-oriented programming, access to an instant variable outside the
class usually is not allowed. Instead, there is provided a method which is
provided to access the instant variable.
Relates to whether or not a variable instan can be accessed from outside the
class, Java provides access determinant.
1. Public means that access an instant variable or method can be done of
the outside the class.
2. Private means that access an instant variable or method only can
be conducted in class.
Differences of public, private and without variable access determinant.
1.

Without access determinant a variable is accessible by the classes


in the same package.

2.

Public variables is accessible at all class containing the same


variable.

3.

Private is only be accessible by the methods in the class its own


class.

Differences in public, private, and without deciding on the access method.

1. Without variable access determinant methodes either can only be


accessed by the method inside its own class or methodes in other class
located on the same package.
2. Public methods can be accessed from any classes
3. Private methods can only be accessed by the methods in the sam class.

C. TOOLS
1. Computer.
2. NetBeans application
D. EXPERIMENT
1. EXPERIMENT 1
1.1. WORK STEPS
1.1.1. Create class named by "Student.java", write program code
as image below.
public class Student {
public String name;
void fillName(String name){
this.name=name;
}
String showName(){
return this.name;
}
}
1.1.2. Compile the codes above
1.1.3. Next create new class "AccessDeterminant.java", and put in
your name.
public class AccessDeterminant {
public static void main(String []args){
Student I=new Student();
I.fillName("Dykson");
System.out.println(I.name());
System.out.println(I.showName());
}
}

1.1.4. Compile the code and analyzes the results


1.2.EXERCISES
1.2.1. Change the access determinant in name variable from
public to private.
public class Student {
private String name;
void fillName(String name){
this.name=name;
}
String showName(){
return this.name;
}
}
1.2.2. Next save with same name and compile the code.
1.2.3. Compile file AccessDeterminant.java.
1.2.4. Note the results and compare with the original results befor
the variable access change to private.
2. EXPERIMENT 2
2.1.WORK STEPS
2.1.1. Create new class named by Circle.java and write program
code as below.
public class Circle {
private double radius;
void fillDigit(double radius){
this.radius=radius;
}
private double takePhi(){
return 3.14;
}
public double calculatePriphery(){
return 2*takePhi()*radius;
}
}
2.1.2. Compile the code above
2.1.3. Next create new class named by "PenentuAksesMethod.java".

public class AccessDeterminantMethode {


public static void main(String []args){
Circle oval=new Circle();
oval.fillDigit(10);
System.out.println("Priphery=
"+oval.calculatePriphery());
System.out.print("Phi value=
"+oval.takePhi());
}
2.1.4. Compile the code
2.2.EXERCISE
2.2.1. Change access deterinant takePhi() method to public
access.
public class Circle {
private double radius;
void fillDigit(double radius){
this.radius=radius;
}
public double takePhi(){
return 3.14;
}
public double calculatePriphery(){
return 2*takePhi()*radius;
}
}

2.2.2. Next save with same name and compile the code.
2.2.3. Compile file AccessDeterminantMethod.java.
2.2.4. Note the results and compare with the original results befor
the variable access change to private.
E. ANALYSIS
For the first experiment the code works properly but since we change the the
access determinant of name variable from public to private as instructed in
the exercise of first experiment. And error is occured.

public class Student {


public String name;
void fillName(String name){
this.name=name;
}
String showName(){
return this.name;
}

public class Student {


private String name;
void fillName(String name){
this.name=name;
}
String showName(){
return this.name;
}

before
for the second experiment is no diffrent with firs experimanet. It is only

after

change the takePhi() variables access deteminant from private to public, so


this time the experiment cunducting error result and after the change the
variables access determinant the code work properly.

public class Circle {


private double radius;
void fillDigit(double radius){
this.radius=radius;
}
private double takePhi(){
return 3.14;
}
public double calculatePriphery()
{
return 2*takePhi()*radius;
}
}

public class Circle {


private double radius;
void fillDigit(double radius){
this.radius=radius;
}
public double takePhi(){
return 3.14;
}
public double
calculatePriphery(){
return 2*takePhi()*radius;
}
}

before

after

Vous aimerez peut-être aussi