Académique Documents
Professionnel Documents
Culture Documents
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.
2.
3.
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());
}
}
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.
before
for the second experiment is no diffrent with firs experimanet. It is only
after
before
after