Public scope means that a object or class scope variable is accessible by any class or object.

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

This code will now work as the variable is now public and thus accessible. It is very common NOT to make attributes public but as a beginer programmer you will most likely make everyting public.

Default scope allows you access to variables if you are in the same package. If classes are in the same package then default will behave like public, otherwise it will behave as private.

To use default you just leave it blank for example -

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

The above code uses default and would only work if foo and the call to foo both happen in the same package,

next names and scope >>

 
Basics
Menu
Search