Modifiers are used to give special meaning to class, interface, fields or methods. There are 2 types of modifiers in Java.
  • Access type of modifiers – used to specify the access level
  • Non-access type of modifiers – used for various purposes
Access Type modifiers
  • public – Public members can be accessed from outside of class.
  • default – This is the default access level. These members can be accessed from all classes within the same package.
  • protected – Protected members can be accessed from outside of class but the condition is that class from which you are accessing it should extend the main class.
  • private – Private members can not be accessed from outside of class. Only methods of class can access it from within the class.
Below example illustrates how access modifiers work. We have 2 classes. Account class is in package modifiers while SimpleTest class is in otherpackage.

package modifiers;

public class Account{
    private double balance;
    protected String accountType;
    public String name;
    String address;

    public Account(){
        this.balance = 0;
        this.accountType = "Savings";
        this.name = "XXX";
        this.address = "Brisbane";
    }

    public void withDrawAmount(double amount){
        this.balance = this.balance - amount;
    }

    public void depositAmount(double amount){
        this.balance = this.balance + amount;
    }

    public double getBalance(){
        return balance;
    }
}

package otherpackage;

import modifiers.*;

public class SimpleTest {

    public static void main(String args []){

        //Account class is in modifiers package. This class (SimpleTest) is in otherpackage.
        Account a = new Account();

        //accountType has a protected access - compilation error
        //as we are accessing protected member from different package
        //System.out.println("account type is -> " +  a.accountType);

        //Can not access the private member from outside of class Account
        //balance has a protected access - compilation error
        //System.out.println("account type is -> " +  a.balance);

        //We can access public members
        System.out.println("Name of the account is -> " +  a.name);
        System.out.println("Balance of the account is -> " +  a.getBalance());
        a.depositAmount(111);
        System.out.println("Balance of the account after depositing 100 is -> " +  a.getBalance());
        a.withDrawAmount(11);
        System.out.println("Balance of the account after withdrawing 11 is -> " +  a.getBalance());

        //We can not access address field of modifiers.Account class from this package
        //System.out.println("address of the account is -> " +  a.address);

    }
}
Here is the output of above code.
  • Name of the account is - XXX
  • Balance of the account is - 0.0
  • Balance of the account after depositing 100 is - 111.0
  • Balance of the account after withdrawing 11 is - 100.0
Non access Type modifiers
  • static – static members can be accessed using Class name. We do not need to create an object of the class to access static members.
  • final – final variables can be assigned value only once. final methods can not be over-ridden.
  • synchronized – Synchronized methods are used when we want thread safety.
  • transient – Transient variables are used when you do not want to store them in a file through serialization process. In short, they are temporary.
  • strictfp – we can mark class or method as strictfp to get the consistent results across different platforms in operations involving floating point arithmetic.
Below example explains how to use various non-access modifiers.

package modifiers;

/**
 * Created by ssalunke on 19/04/2016.
 */
public class NonAccessModifiers {

    public static void main(String [] args){

        //static, final, abstract, synchronized, volatile, transient
        //We can access static method and variables using class name
        //We do not need to create objects of classes to access them
        A.printStatic();
        System.out.println("Value of size " + A.size);

        A a1 = new A();

        //Below line will throw compilation error saying
        //Can not assign a value to final variable count
        //a1.count++;

        //Another thread will not be allowed to invoke this method simultaneously
        a1.changeName("New Name");
    }
}

class A{
    static int size =10;
    final int count = 1;
    String name;

    //Transient variables will not be saved during serialization process
    transient int temp;

    //Variable d is volatile which means that d will be read and written to main memory
    //and not from cpu cache
    volatile double d;

    public static void printStatic(){
        System.out.println("This is static method @ class level");
    }

    final void printFinal(){
        System.out.println("This is final method. Do not try to override");

        //Below line will throw compilation error saying
        //Can not assign a value to final variable count
        //count++;
    }

    //This method is synchronized which means it is thread safe
    synchronized public void  changeName(String name){
        this.name = name;
    }

}

class B extends A{

    //Below line will throw compilation error saying
    //Can not override printFinal as it is final method
    //public void printFinal(){ }
}

Here is the output of above code.

This is static method @ class level
Value of size 10
Complex problems, Simple Solutions