Java Basics:Java Non Access Modifiers-1
- Get link
- X
- Other Apps
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 used for creating a static variable and static method. The reserved keyword for a static modifier is static, which is used before the data type of the 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 the class name without creating an instance or object of this class.
For example, in theCar.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
public class Car{
public static String msg; //String type static variable is declared
public static void create(){
System.out.print(msg); //Variable msg is passed as argument to print.
}
}
In the Main.java
class, at line 6, without creating any object of class Car
, we initialized the static variable msg
in the Car
class and invoked create()
method in class Main
defined in the Car
class. Syntax is ClassName.Method
.
Main.java
public class Main{ //initialization of class
public static void main(String[] args){
//value is stored in static variable
Car.msg = "A new car has been created";
// static method in class Car is invoked
Car.create();
}
} //end of class

Final Modifier
In Java, variable, method, and class can have a final non-access modifier. The reserved keyword for a 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 that is uninitialized is called a blankfinal variable that can be initialized only in the constructor of a class. To make a class variable, sometimes, we use the static keyword with final
. If a static final variable is not initialized, we can initialize it in a static block.
Code Example for Final Variable With Error:
public class Main{ //initialization of class
//declare a final variable of int data type
public final int number = 1;
//non-static method to print number value
public void print(){
System.out.print(number); //print number
number = 2; //change value of number
System.out.print(number); //print new value
}
// main method
public static void main(String[] args){
//object of class Main to access non-static methods
Main p = new Main();
//print method invoked
p.print();
}
} //end of class

If we remove code at line 11, the modified code would be executed without any error.
Code Example for Final Variable:
public class Main{ //initialization of class
//declare a final variable of int data type
public final int number = 1;
//non-static method to print number value
public void print(){
System.out.print("Output is "+number); //print number
}
// main method
public static void main(String[] args){
//object of class Main to access non-static methods
Main p = new Main();
//print method invoked
p.print();
}
} //end of class
Code Example for Static Final Variable:
public class Main{ //initialization of class
//declare a static final variable of int data type
public static final int number = 1;
// main method
public static void main(String[] args){
System.out.print("Output is "+number); //print number
}
} //end of class
A method that we cannot override is called finalmethod
. It can be inherited in any other class.
Code Example for Final Method:
Car.java
public class Car{
//final method
public final void create(){
System.out.print("I'm final method to create a car.");
}
}
Main.java
//initialization of class
public class Main extends Car{
//non-static method to access non-static methods in Car.java
public void makeCar(){
create(); //method invoked
}
// main method
public static void main(String[] args){
Main m = new Main(); //object of Main class
m.makeCar(); //call to makeCar method
} //end of main method
} //end of class
A class that we cannot extend is called finalclass
. It means a final class cannot be inherited by any other class.
Code Example for Final Class:
Car.java
public final class Car{
//void method
public void print(){
//method body
}
}
Main.java
//initialization of class
public class Main extends Car{
public static void main(String[] args){
} //end of main method
} //end of class
Abstract Modifier
A class or method can be declared as abstract. Reserved keyword abstract
is used for the 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 the sole purpose of overriding the method body. To make a class abstract, at least one abstract method should be defined in a class. If we have to declare abstract methods in a class, a class must be an abstract class.
An abstract method is just a declaration; it does not contain an implementation or method body. You will have to define its implementation by overriding this method in a class, which has extended the abstract class. When a class extends an abstract class, all the abstract methods defined in the abstract class must be overridden in a subclass.
Car.java
public abstract class Car{
public abstract void create();
public void print(){
System.out.print("I'm not abstract method.");
}
}
Main.java
//initialization of class
public class Main extends Car{
// overriding method defined in Car class
//as create is only abstract method in Car class
//overriding it is compulsory
//otherwise error will occur
@Override
public void create(){
System.out.println("I'm overridden method.");
}
// main method
public static void main(String[] args){
Main m = new Main(); //object of Main class
m.create(); //invoking overridden method
m.print();
} //end of main method
} //end of class
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 to 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.
public class Salary implements Runnable {
private volatile int salary;
public void run() {
salary = 5000; //assigning value to salary
do{
//loop body
}
while (salary <= 5000); // while loop definition
}
public void stop() {
salary = 5001; // new value assignment to salary
}
}
Transient Modifier
When we use a transient modifier in an instance variable declaration, the JVM will skip this variable or field while serializing the object containing that variable. Reserved keyword is transient
.
public abstract class Car{
//instance variable with transient modifier
public transient int number = 22114;
}
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
.
The synchronized modifier can be used with any access modifier.
public abstract class Car{
public synchronized make(){
//method body
}
}
That's all for this post on access and non-access modifiers in Java.
Happy modifying!
- Get link
- X
- Other Apps
Comments
Post a Comment