Access modifiers change who can call or change variables or methods within a class or object.There are three different access modifiers -

 

public foo(
    private int a 5;
    public void method(){
        System.out.println(a);
     }
}
foo myFoo = foo();
myFoo.method();
myFoo.a = 4;

The last line of this code will fail. Private attributes can not be called from outside the class. Private allows programmers to hide away details of their code and the inner workings of their classes.

next public and default >>

 
Basics
Menu
Search