Modifier in java With Programming Examples
Description:
Modifier in java: in this article, I am going to show you how to use different access modifiers in java. The main modifiers are public, private, protected, static, final, abstract, transient, volatile, synchronized, and native.
Modifier in java:
Java provides modifiers for many areas. These can be used to mark classes, attributes, and/or methods for various purposes.
The modifiers are: public, private, protected, static, final, abstract, transient, volatile, synchronized, and native. The most well-known modifiers are probably the public, private, and protected access modifiers, which are used to control the visibility of the class, attribute, or method.
The following overview shows you which areas each modifier is used for:
Attribute | Method | Constructor | Class | Interface | |
abstract | X | X | x | ||
final | X | X | X | ||
native | X | ||||
private | X | X | X | ||
protected | X | X | X | ||
public | X | X | X | x | x |
static | X | X | |||
synchronized | X | ||||
transient | X | ||||
volatile | X |
Abstract access modifier in java
The abstract access modifier in java can be used on a method, class, or interface. This means that the element marked with abstract has not yet been fully implemented. If a method or an interface has the modified abstract, the implementing class must also be declared as abstract. If a class is marked with abstract, no object can be created from it.
A subclass derived from an abstract class must fully implement all abstract methods of the parent class, otherwise, it must also be declared an abstract modifier. In contrast to an interface, an abstract class can also contain implemented methods and attributes (that are not declared as static or final ). If there is no implementation at all for an abstract class, it makes more sense to declare it as an interface.
Example: how to use abstract modifier in java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
abstract class Show{ abstract void Message(); } class Test extends Show { void Message() { System.out.println("Programming Digest"); } public static void main(String args[]){ Show obj = new Test(); obj.Message(); } } |
output:
The class Show contains the abstract method Message. This method is marked with abstract and therefore has no method body. The method header ends directly with a semicolon. Because the Message method is abstract, the Show class must also be abstract.
final access modifier in java:
The final access modifier in java can be used on attributes, methods, and classes. Attributes with final are constants and can no longer be changed. Methods that have the modifier final cannot be overridden when inherited. A class, on the other hand, is at the end of an inheritance hierarchy if it is declared as final. No further subclasses can be derived from it.
Example:
// Constant PI can no longer be changed
final double PI = 3.14 ;
The example above shows the use of final. In this case, it is a constant that can no longer be changed.
Example: how to use final modifier in java:
1 2 3 4 5 6 7 8 9 10 11 |
class Test{ public static void main(String[] args) { // create a final variable final int id=1 ; // try to change the final variable id = 2; System.out.println("ID: " + id); } } |
Output:
You will get compile-time error
Native modifier:
Only methods can be marked with the modifier native. These methods are written in another programming language and are used for “multilingual” communication. Since native is more likely to be used in the more professional area, we will not give an example at this point.
Private access modifier in java
The private access modifier in java can be applied to attributes, methods, and constructors. These cannot then be seen and used by another class (not even by derived classes). For better data encapsulation, attributes should be provided with this modifier as far as possible.
Example: how to use private access modifier in java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Message{ // private variable private String message; } public class Test{ public static void main(String[] main) { // create an object of Message class Message msg = new Message(); // access private variable and field from another class msg.message = "Hello programming digest"; } } |
output:
You will get the following error while compiling the code because we cannot access the private variable data in the Test class
Protected access modifier in java
The protected access modifier in java can be used in the same way as private, but here the visibility is limited to the package and not just to your own class, as with private. Subclasses could also always “see” the members of the methods declared protected.
 Example: how to use protected access modifier in java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class vehicle { // protected method protected void car() { System.out.println("this is protected car method"); } protected void bike() { System.out.println("this is protected bike method"); } } class Test extends vehicle { public static void main(String[] args) { // create an object of Test class Test obj= new Test(); // access protected method obj.car(); obj.bike(); } } |
output:
Public access modifier in java
The public access modifier is allowed for all constructs. The elements declared in this way can be used by any class, so they are public. Attributes of your own class that are marked private can also be read out and changed via public methods.
Example: how to use public access modifier in java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class Message{ // public variable public String msg; // public method public void display() { System.out.println("Fawad khan"); System.out.println("Hello" + msg); } } public class Test{ public static void main( String[] args ) { // accessing the public class Message obj= new Message(); // accessing the public variable obj.msg =" Programming digest"; // accessing the public method obj.display(); } } |
output:
Static modifier:
Class variables or class methods are marked with the modifier static.
Class methods are independent of objects. The static method is therefore called via the class name and not via the object name or this pointer.
Unlike attributes, class variables have the same value for every object in the class. In a sense, the instances of the class share the variable. If the class variable is changed in one object, all other objects have also received the change.
Example: how to use static keyword modifier in java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Test { // static method static void Message() { System.out.println("Hello Programming digest, i am from static method"); } public static void main(String[] args) { // calling Message without creating // any object of class Test Message(); } } |
output:
Synchronized modifier:
Only methods are marked with the synchronized modifier in java. This ensures data consistency when using threads if one thread wants to read and another thread wants to write to data.
Example: how to use Synchronized modifier in java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
class Table{ synchronized void Display(int n){ for(int i=1;i<=10;i++){ System.out.println(n*i); try{ Thread.sleep(400); }catch(Exception e){System.out.println(e);} } } } class Thread1 extends Thread{ Table t; Thread1(Table t){ this.t=t; } public void run(){ t.Display(10); } } class Thread2 extends Thread{ Table t; Thread2(Table t){ this.t=t; } public void run(){ t.Display(100); } } public class Test{ public static void main(String args[]){ Table obj = new Table(); Thread1 t1=new Thread1(obj); Thread2 t2=new Thread2(obj); t1.start(); t2.start(); } } |
output:
Transient modifier:
Attributes marked with the modifier transient are not taken into account during serialization, which means, for example, that these attributes are not written to a file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.io.*; class Test implements Serializable { int a = 1, b = 2; transient int c = 30; transient static int d = 3; transient final int e = 4; public static void main(String[] args) throws Exception { Test input = new Test(); FileOutputStream fos = new FileOutputStream("xyz.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(input); FileInputStream fis = new FileInputStream("xyz.txt"); ObjectInputStream ois = new ObjectInputStream(fis); Test output = (Test)ois.readObject(); System.out.println("a = " + output.a); System.out.println("b = " + output.b); System.out.println("c = " + output.c); System.out.println("d = " + output.d); System.out.println("e = " + output.e); } } |
output:
volatile modifier:
Attributes marked with the modifier volatile allow concurrent access by two or more threads.
Example: how to use volatile modifier in java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public class Test{ private static int number; private volatile static boolean ready; private static class Reader extends Thread { @Override public void run() { while (!ready) { Thread.yield(); System.out.println("not ready"); } System.out.println(number); } } public static void main(String[] args) { new Reader().start(); number = 20; ready = true; } } |
output:
When the ready is true you will see the following output.
And when you change the ready from  true to false you will see the following output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public class Test{ private static int number; private volatile static boolean ready; private static class Reader extends Thread { @Override public void run() { while (!ready) { Thread.yield(); System.out.println("not ready"); } System.out.println(number); } } public static void main(String[] args) { new Reader().start(); number = 20; ready = false; } } |
output: