Access and Non-Access Modifiers in Java – Easy Java Tutorial
In Java, a modifier has a reserved keyword which is included in the definition of class, method and variables. A modifier adds some meanings to these definitions. Modifiers are also called specifiers.
These modifiers are classified into two categories. Some of the modifiers are called access modifiers and some are called non-access modifiers.
Access modifiers are reserved keywords that provide different level of access to classes, methods, fields etc. Reserved keywords for access modifiers are public, protected and private.
Modifiers in Java:
Access modifiers are further classified into the following categories:
- Default Access Modifier
- Public Access Modifier
- Protected Access Modifier
- Private Access Modifier
You may be interested in studying this Java Tutorial.
Default Access Modifier:
Default Access Modifier does not require to use any reserved keyword. Any method or variable with default access modifier can be accessed from any class in a package. It behaves almost similar as public access modifier but there is a difference between them. In an interface, variables are public static final and methods are public by default.
1 class Car{ //initialization of class 2 3 Car object1; // object declaration 4 5 6 void moveForward(){ //void method 7 //method body 8 } 9 10 } //end of class 11
Public Access Modifier:
In Public Access Modifier, a keyword public is used before variable, method, class or constructor. A public method or variable can be accessed in any class even the class belongs to different package. If a class belongs to a different package, we can import it using inheritance.
1 public class Car{ //initialization of class 2 3 public Car object1; // object declaration 4 5 6 public void moveForward(){ //void method 7 //method body 8 } 9 10 } //end of class 11
Protected Access Modifier:
In Protected Access Modifier, a keyword protected is used before variable, method and constructor. No class can have protected access modifier. Any variable, method or constructor with protected access modifier is only accessible within child classes or subclasses of super classes in which it is declared.
1 public class Car{ //initialization of class 2 3 protected Car object1; // object declaration 4 5 6 protected void moveForward(){ //void method 7 //method body 8 } 9 10 } //end of class 11
You may be interested in studying article on Cyber Security TRIADS
Private Access Modifier:
In Private Access Modifier, a keyword private is used before variable, method and constructor. A class and interface cannot be private. A class can have private access modifier only if it is inner class i.e. a class is member of another class. Private provides most restricted level of access. To access any variable or field outside the class in which it is declared, setter and getter methods are used. Getter methods are used to access variable outside class and setter methods are used to set values for these private fields in a class, where these are declared.
1 public class Car{ //initialization of class 2 3 private Car object1; // object declaration 4 5 6 private void moveForward(){ //void method 7 //method body 8 } 9 10 11 12 private class Horn{ //inner class with private access modifier 13 14 } 15 16 } //end of class
Non-Access Modifiers:
Non-Access Modifiers are further classified as:
- Static Modifier
- Final Modifier
- Abstract Modifier
- Synchronized Modifier
- Transient Modifier
- Volatile Modifier
Static Modifier:
Static Modifier is for creating static variable and static method. The reserved keyword for static modifier is static which is used before data type of variable or method. A static method or variable exists independently of any class object. It means a static variable or static method can be called using class name without creating instance or object of this class.
For example, Car.java class below, at line 3, a static variable msg is declared. At line 5, a void method is declared which will print the value of static variable msg on screen. Syntax is ClassName.Variable.
Car.java
1 public class Car{ 2 3 public static String msg; //String type static variable is declared 4 5 public static void create(){ 6 7 System.out.print(msg); //Variable msg is passed as argument to print. 8 } 9 10}
In Main.java class, at line 6, without creating any object of class Car, we initialized static variable msg in Car class and invoked create() method in class Main defined in Car class. Syntax is ClassName.Method.
Main.java
1 public class Main{ //initialization of class 2 3 public static void main(String[] args){ 4 5 //value is stored in static variable 6 Car.msg = "A new car has been created"; 7 8 // static method in class Car is invoked 9 Car.create(); 10 11 } 12 } //end of class
Final Modifier:
In Java, variable, method and class can have final non-access modifier. The reserved keyword for final non-access modifier is final. This keyword is used to make any class, method or variable final.
Once a final variable is initialized, you cannot change its value again. A final variable which is uninitialized is called blank final variable that can be initialized only in constructor of class. To make a class variable, sometimes, we use static keyword with final. If a static final variable is not initialized, we can initialize it in static block.
Code Example for final variable with Error:
1 public class Main{ //initialization of class 2 3 //declare a final variable of int data type 4 public final int number = 1; 5 6 //non-static method to print number value 7 public void print(){ 8 9 System.out.print(number); //print number 10 11 number = 2; //change value of number 12 13 System.out.print(number); //print new value 14 15 } 16 17 // main method 18 public static void main(String[] args){ 19 20 //object of class Main to access non-static methods 21 Main p = new Main(); 22 //print method invoked 23 p.print(); 24 25 } 26 } //end of class 27
If we remove code at line 11, the modified code would be executed without any error.
Code example for final variable:
1 public class Main{ //initialization of class 2 3 //declare a final variable of int data type 4 public final int number = 1; 5 6 //non-static method to print number value 7 public void print(){ 8 9 System.out.print("Output is "+number); //print number 10 11 } 12 13 // main method 14 public static void main(String[] args){ 15 16 //object of class Main to access non-static methods 17 Main p = new Main(); 18 //print method invoked 19 p.print(); 20 21 } 22 } //end of class
Code example for static final variable:
1 public class Main{ //initialization of class 2 3 //declare a static final variable of int data type 4 public static final int number = 1; 5 6 // main method 7 public static void main(String[] args){ 8 9 System.out.print("Output is "+number); //print number 10 11 } 12 } //end of class 13
A method which we cannot override is called final method. It can be inherited in any other class.
Code example for final method:
Car.java
1 public class Car{ 2 3 //final method 4 public final void create(){ 5 6 System.out.print("I'm final method to create a car."); 7 } 8 9}
Main.java
1 //initialization of class 2 public class Main extends Car{ 3 4 //non-static method to access non-static methods in Car.java 5 public void makeCar(){ 6 create(); //method invoked 7 } 8 // main method 9 public static void main(String[] args){ 10 11 Main m = new Main(); //object of Main class 12 m.makeCar(); //call to makeCar method 13 14 } //end of main method 15 } //end of class 16
A class which we cannot extend is called final class. It means a final class cannot be inherited by any other class.
Code example for final class:
Car.java
1 public final class Car{ 2 3 //void method 4 public void print(){ 5 //method body 6 } 7 8}
Main.java
1 //initialization of class 2 public class Main extends Car{ 3 4 5 public static void main(String[] args){ 6 7 8 9 } //end of main method 10 } //end of class
Abstract Modifier:
A class or method can be declared as abstract. Reserved keyword abstract is used for abstract modifier.
An abstract class or method cannot be final because an abstract class is made with the purpose of extending it in other classes and an abstract method is made for sole purpose of overriding the method body. To make a class abstract, at least one abstract method should be defined in class. If we have to declare abstract methods in a class, a class must be abstract class.
An abstract method is just declaration, it does not contain implementation or method body. You will have to define its implementation by overriding this method in a class which has extended abstract class. When a class extends abstract class, all the abstract methods defined in abstract class must be overridden in subclass.
Car.java
1 public abstract class Car{ 2 3 public abstract void create(); 4 public void print(){ 5 6 System.out.print("I'm not abstract method."); 7 8 } 9 10}
Main.java
1 //initialization of class 2 public class Main extends Car{ 3 4 // overriding method defined in Car class 5 //as create is only abstract method in Car class 6 //overriding it is compulsory 7 //otherwise error will occur 8 @Override 9 public void create(){ 10 11 System.out.println("I'm overridden method."); 12 13 } 14 15 // main method 16 public static void main(String[] args){ 17 18 Main m = new Main(); //object of Main class 19 m.create(); //invoking overridden method 20 m.print(); 21 22 } //end of main method 23 } //end of class 24
Volatile Modifier:
This modifier is applied only to private or object type instance field or variable. Reserved keyword for this modifier is volatile.
It indicates the JVM to merge a thread’s own copy of field with the master copy of field available in memory. When we access a volatile variable, it synchronizes all the available cached copies of variables in memory.
1 public class Salary implements Runnable { 2 private volatile int salary; 3 4 public void run() { 5 salary = 5000; //assigning value to salary 6 do{ 7 8 //loop body 9 10 } 11 while (salary <= 5000); // while loop definition 12 } 13 14 public void stop() { 15 salary = 5001; // new value assignment to salary 16 } 17}
Transient Modifier:
When we use a transient modifier in an instance variable declaration, JVM will skip this variable or field while serializing the object containing that variable. Reserved keyword is transient.
1 public abstract class Car{ 2 3 //instance variable with transient modifier 4 public transient int number = 22114; 5 6}
Synchronized Modifier:
A modifier that is used to restrict a method to be used by any other thread, when it is under use of one thread. Reserved keyword for this modifier is synchronized.
Synchronized modifier can be used with any access modifier.
1 public abstract class Car{ 2 3 public synchronized make(){ 4 //method body 5 } 6}