Access Modifiers
Home Up Access Modifiers Arrays Simple Data Types

 

Accessibility

  1. public -The variable is accessible from anywhere.
  2. protected - The variable is accessible anywhere within the declared class or one of its subclasses.
  3. private - The variable is accessible only within its declared class.
  4. nothing specified - The variable is accessible within the file of the class where it is declared.

e.g.   public int numberDice;             // public access
         protected int numberDice;       // protected access
         private int numberDice;           // private access
         int numberDice;                       // default access

Final

The final keyword indicates that a variable is being given a value that can not be changed.

e.g.    final int numberDice = 2;     // the value can not be changed
          int numberDice = 2;              // the value can be changed

Static

The static keyword indicates that only one instance of the variable will be created and then shared among every instance of a class.

e.g.   static int numberDice;
         int numberDice; 

In the first example, every instance of a class shares the same variable.  If one instance changes the value of numberDice, it changes for all other instances.  In the second example, every instance of a class gets its own version of  the variable.   Thus, each instance could potentially have a different value for the numberDice variable.