Access modifier:

Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control: 1. At the top level-public, or package-private (no explicit modifier). 2. At the member level-public, private, protected, or package-private (no explicit modifier).
A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes - you will learn about them in a later lesson.)
At the member level, you can also use the public modifier or no modifier (package-private) just as with top-level classes, and with the same meaning. For members, there are two additional access modifiers: private and protected. The private modifier specifies that the member can only be accessed in its own class. The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.
accessmodifers

Below program is an example for how to declare variables with access modifiers.

     public class Test
     {
	private int i;
	int j;
	protected int k;
	public int l;	
    }

A sample program on access modifiers:

       package com.techpalle.package1;

        public class One    
        {
	private int i = 10; //private
	int j = 20; //default
	protected int k = 30; //protected
	public int l = 40;//public
	
	   void display()   
             {
		System.out.println(i);
		System.out.println(j);
		System.out.println(k);
		System.out.println(l);
	      }
          }
                 class Two
                 {
	         public void fun()
                 {
		  One o = new One();
	          //System.out.println(o.i);//error as it's private
		  System.out.println(o.j);
		  System.out.println(o.k);
		  System.out.println(o.l);
	          }
                 }

 package com.techpalle.package2;

 import com.techpalle.package1.One;

       public class Three extends One
       {
       public static void main(String[] args)
       {
		
        }
       public void test()
       {
         //System.out.println(i);//error as it is private
	 //System.out.println(j);//error as it is default
           System.out.println(k);
           System.out.println(l);
         }
      }