You are on page 1of 17

OBJECT-ORIENTED PROGRAMMING

WEEK 11:
Non-Access Modifiers in Java
Learning Objectives:
 To use the different access modifiers in Java.
 To distinguish between access modifier and

non-access modifier.
Access and Non-Access Modifiers in Java
 In Java, a modifier has a reserved keyword that 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 a different


level of access to classes, methods, fields, etc. Reserved keywords
for access modifiers are public, protected, and private.
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.
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.
}
}

Test.java OUTPUT:
public class Test{ //initialization of class A new car has been created
public static void main(String[] args){
//value is stored in static variable
Car.msg = "A new car has been created"; In the Test.java class, we initialized the
// static method in class Car is invoked static variable msg in the Car class
Car.create(); and invoked create() method in
class Main defined in the Car class without
} creating any object of class Car,
} //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.
Test2.java 1ST OUTPUT:
public class Test2{ //initialization of class Test2.java:9: error: cannot assign a value to
final variable number
number = 2; //change value of
//declare a final variable of int data type number
public final int number = 1;
COMPILE TIME ERROR DUE TO
//non-static method to print number value REASSIGNING VALUE TO FINAL
public void print(){ VARIABLE.
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){ 2ND OUTPUT:
//object of class Test2 to access non-static methods 11
Test2 t2 = new Test2();

//print method invoked


t2.print(); IF WE REMOVE CODE IN RED,
} number = 2;
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.
Car.java Test.java
public abstract class Car3{ //initialization of class
public abstract void create(); public class Test extends Car{
public void print(){
System.out.print("I'm not abstract method."); // 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.");
}
OUTPUT:
Test.java:16: error: Car is abstract; // main method
public static void main(String[] args){
cannot be instantiated
Car m = new Car(); //object of Main class
Car m = new Car(); //object of m.create(); //invoking overridden method
Main class 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.
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{

//instance variable with transient modifier

public synchronized make(){


//method body

}
}
END OF PRESENTATION…

You might also like