Access and Non-Access Modifiers in Java

Modifiers in Java:

  1. Default Access Modifier
  2. Public Access Modifier
  3. Protected Access Modifier
  4. Private Access Modifier

Default Access Modifier:

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:

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:

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

Private Access Modifier:

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:

  1. Static Modifier
  2. Final Modifier
  3. Abstract Modifier
  4. Synchronized Modifier
  5. Transient Modifier
  6. Volatile Modifier

Static Modifier:

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}
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:

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

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

Code example for final method:

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}
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

Code example for final class:

1 public final class Car{ 
2
3 //void method
4 public void print(){
5 //method body
6 }
7
8}
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:

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}
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
3 } //end of class
24

Volatile Modifier:

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:

1 public abstract class Car{ 
2
3 //instance variable with transient modifier
4 public transient int number = 22114;
5
6}

Synchronized Modifier:

1 public abstract class Car{ 
2
3 public synchronized make(){
4 //method body
5 }
6}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Arslan ud Din Shafiq

Arslan ud Din Shafiq

Chief Executive Officer at WebSoft America Inc., Software Engineer, Books Author | https://websoft.ltd | https://www.itdevsols.com | https://www.imarslan.net