Java Basics:Java Non Access Modifiers-1

 Non-Access Modifiers

Non-Access Modifiers are further classified as:

  1. Static Modifier
  2. Final Modifier
  3. Abstract Modifier
  4. Synchronized Modifier
  5. Transient Modifier
  6. 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


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



static-access-modifierOUTPUT 2 – MAIN.JAVA

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:


final-access-modifierCOMPILE TIME ERROR DUE TO REASSIGNING VALUE TO FINAL VARIABLE


If we remove code at line 11, the modified code would be executed without any error.

Code Example for Final Variable:

final variable

Code Example for Static Final Variable:


A method that we cannot override is called finalmethod. It can be inherited in any other class.

Code Example for Final Method:

Car.java


Main.java

final-method-example-in-java

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


Main.java

final-class-example

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


Main.java

abstract-class-and-abstract-method

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.


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.


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.


That's all for this post on access and non-access modifiers in Java. 

Happy modifying!

Comments

Popular posts from this blog

Java OOPS:OOPS Concepts Overview

Java OOPS:Constructors in Java – A complete study!!

Java Basics:Data Types