As we discussed in Access Modifiers, Non-Access Modifiers do not change any accessibility of methods or variables. But they are providing special capabilities to them.
There are various types of Non-Access modifiers are available in Java.
1) Final: It is applicable to class, method and variable.
a) Final Class: Such class can not be extended by any other class.
b) Final Method: Such method can not be overridden by any subclass.
c) Final Variable: Such variable's value can not changed. They are act like constants.
public final class JavaFixer { // final class final int COUNT = 10; // final variable final void displayCount() // final method { System.out.println("Count is" + COUNT); } }
2) Static: It is applicable to Nested class, method and variable.
a) Static Nested Class: Such class can be accessed without any instance of class.
b) Static Method: Such method can be accessed without any instance of class.
c) Static Variable: Such variable's has only one single storage. Means its value will be shared among all objects of variable's class.
public class JavaFixerOuter { static int count = 10; // static variable static void displayCount() // static method { System.out.println("Count from displayCount method: " + count); } static class JavaFixerInner { // static nested class static void printMessage() { System.out.println("Count from JavaFixerInner class: " + count); } } } public class Main { public static void main(String[] args) { System.out.println("Count = " + JavaFixerOuter.count); JavaFixerOuter.displayCount(); JavaFixerInner.printMessage(); } }
3) Abstract: It is applicable to class and method.
a) Abstract Class: Such class must be declared abstract. It may or may not contain abstract methods.
b) Abstract Method: Such method does not contain method body.
public abstract class JavaFixer { // abstract class abstract void abstractMethod(); // abstract method } public class Main extends JavaFixer { void abstractMethod() { System.out.println("Hello from abstractMethod"); } }
4) Synchronized: It is applicable to method.
a) Synchronized Method: Such method can be accessed by only one thread at time.
public class JavaFixer { private int count = 0; public synchronized void increment() { // synchronized method count++; } public synchronized int getCount() { // synchronized method return count; } }
a) Native Method: Such method represent that its implementation has been done on platform dependent code.
public class JavaFixer { private native void print(); public static void main(String[] args) { new JavaFixer().print(); } }
6) Strictfp: It is applicable to class and method.
a) Strictfp Class: Such class gives assurance that all operations on floating point variables of class will return same result on all platform.
b) Strictfp Method: Such Method gives assurance that all operations on floating point variables of method will return same result on all platform.
public strictfp class JavaFixer { // strictfp class private strictfp void print(){} // strictfp method }
7) Transient: It is applicable to variable.
a) Transient Variable: Such variable value doesn't persist when object is serialized.
public class JavaFixer implements Serializable { private static final long serialVersionUID = 1L; transient int count = 10; // transient variable }
8) Volatile: It is applicable to variable.
a) Volatile Variable: Such variable indicate that a variable's value will be modified by different threads. Its value will never be cached in CPU.
public class JavaFixer { volatile int count = 10; // volatile variable }