Packages

The scope of a private variable in only in the class in which it is declared.
The scope of default or friendly is in all classes in the same package either by
a) Inheritance OR     b) by creating an instance

The visibility of the protected is like default as well as in another class in another package only if there is an inheritance and a instance of sub class is mandatory for non static variables (static variables can be called without creating an instance)
The visibility of public is in all classes in any Package.

package One;
public class Pack1
 {
    private int x1 = 10;
    static int x2 = 20;
    protected int x3 = 30;
    protected static int x4 = 40;
    public int x5 = 50;
}

Save this file as Pack1.java under the same folder     : c:\students\Maya>
Set the path                        : c:\students\Maya>set path=c:\jdk1.3\lib
Compile the file                    : c:\students\Maya>javac –d . Pack1.java
The above command creates One folder under Maya and saves compiled file Pack1.class in One.

package One;
class Pack2 extends Pack1
{
    public static void main(String [] args)
    {
        //System.out.println(x1);
                System.out.println(x2);
        //System.out.println(x3);
        System.out.println(x4);
        //System.out.println(x5);
    }
}

Save this file as Pack2.java under the same folder     : c:\students\Maya>
Compile the file                    : c:\students\Maya>javac –d . Pack2.java
Run the file                         : c:\students\Maya>java One.Pack2

OUTPUT : Compile Error –     x1    private access in One.Pack1
                x3    non static variable can not be referenced from a static
                x5    non static variable can not be referenced from a static
Hence comment these 3 lines using “//” and save and compile Pack2.java file.

OUTPUT :    
        20
        40

package One;
class Pack3
{
    public static void main(String [] args)
    {
        Pack1 m = new Pack1();
        //System.out.println(m.x1);
         System.out.println(m.x2);
        System.out.println(m.x3);
        System.out.println(m.x4);
        System.out.println(m.x5);
    }
}

OUTPUT : Compile Error -     x1    private access in One.Pack1

Non static variables x3 and x5 are getting printed because instance of Pack1 class is created to refer to them. Non static variables can be called in static methods by referring them by the instance. Comment x1 and then save the file.
Save this file as Pack2.java under the same folder     : c:\students\Maya>

Compile the file                    : c:\students\Maya>javac –d . Pack3.java
Run the file                         : c:\students\Maya>java One.Pack3

OUTPUT :     20    30    40    50

package Two;
import One.*;
class Pack4
{
    public static void main(String [] args)
    {
        Pack1 n = new Pack1();
        //System.out.println(n.x1);
                //System.out.println(n.x2);
        //System.out.println(n.x3);
        //System.out.println(n.x4);
        System.out.println(n.x5);
    }
}

Save this file as Pack4.java under the same folder     : c:\students\Maya>
Compile the file                    : c:\students\Maya>javac –d . Pack4.java
Run the file                        : c:\students\Maya>java Two.Pack4
The above command creates Two folder under Maya and saves compiled file Pack1.class in Two.

OUTPUT : Compile Error – n.x1    private access in One.Pack1
            n.x2    not public in Pack1. default has a scope within same package
n.x3    has a protected access in Pack1. Protected has a scope within the same package & in another package only inherited class   
n.x4    on has a protected access in Pack1. Protected has a scope within the same package & in another package only inherited class

package Two;
import One.*;
class Pack5 extends Pack1
{
    public static void main(String [] args)
    {
        Pack1 p = new Pack1();
        //System.out.println(p.x1);
                //System.out.println(p.x2);
        //System.out.println(p.x3);
        System.out.println(p.x4);
        System.out.println(p.x5);
       
        Pack5 p1 = new Pack5();
        //System.out.println(p1.x1);
                //System.out.println(p1.x2);
        System.out.println(p1.x3);
        System.out.println(p1.x4);
        System.out.println(p1.x5);

    }
}

Save this file as Pack5.java under the same folder     : c:\students\Maya>
Compile the file                    : c:\students\Maya>javac –d . Pack5.java
Run the file                        : c:\students\Maya>java Two.Pack5

OUTPUT : Compile Error – p.x1    private access in One.Pack1
            p.x2    not public in Pack1. default has a scope within same package
p.x3    has a protected access in Pack1. Protected has a scope within the same package & in another package only inherited class. But the non static variables can be referenced only by the instance of subclass   
p1.x1    private access in One.Pack1
p1.x2    not public in Pack1. default has a scope within same package

OUTPUT : After commenting the above lines : 40    50    30    40    50

Save all java files under same folder c:\students\Maya. Compile files Pack1.class, Pack2.class and Pack3.class get created in folder One whereas Pack4.class and Pack5.class get created in folder Two.