Java – Non-Access Modifiers

Non-access modifiers in Java

Java provides the following four non-access modifiers

  1. Static Modifier (static keyword)
  2. Final Modifier (final keyword)
  3. Abstract Modifier (abstract keyword)
  4. Synchronized Modifier (synchronized keyword)
  5. Volatile Modifier (volatile keyword)
  6. Transient Modifier(transient keyword)

 

1. Static Modifier (static keyword)

Static modifier applies to variables and method. Does not apply to classes or interfaces. Let’s consider static variables and static class

 

(a) Static Variable

Static keyword is used to create static variable. A static variable does not depend on the objects or instances of the class. It is independent. Only one copy static variable exists even if there are many instances of the class.

Static variables are also called class variables. This is because they belong to the class, not to objects. Also note that you cannot declare local variables as static

 

(b) Static Methods

Static keyword is used to  create static methods. Just like static variable, static method is independent of instances of the class. They are also called class methods.

You can access class variables and methods using the syntax <class_name>.<method_name>

The example of Listing 1.0 illustrate how static variable works. I recommend you copy and run it to see the output. Note that throughout the program, it’s just one numberOfShapes variable that exists.

 

public class Shape {
	static int numberOfShapes = 0;
	
	public Shape() {
		numberOfShapes++;
	}
	
	public static void main(String[] args) {
		
		Shape shape1 = new Shape();
		Shape shape2 = new Shape();
		
		System.out.println(numberOfShapes);
		
		System.out.println(Shape.numberOfShapes);
		
		Shape.numberOfShapes = 20;
		System.out.println(numberOfShapes);
	}
}

Listing 1.0: Example of Static Variable

 

2. Final Modifier (final keyword)

The final modifier can be applied to classes, methods and variables.

 

(a) Final Variables

If you declare a variable as final, then it is initialized only once. So you cannot change the value later. A final reference variable can never be re-assigned to refer to another object.

Let’s take an example.

public class Tester {

	final int pay = 1000;

	public static final double OVERTIME = 200.00;
	
	public static void main(String[] args) {
		Tester t = new Tester();
		t.pay = 45; //gives error
		
		OVERTIME = 300; //Error..you cannot re-assign final variable
	}
}

Listing 1.1: Final variables

 

I recommend you try to run this code. You will notice the compiler error message. You cannot assign a final variable

 

(b) Final Methods

You use final for methods that need not be override in the subclass. Therefore, you cannot modify a final method in a child class.  You can make a method final by adding the final keyword to it.

 

(c) Final Classes

A final class is a class that cannot be subclassed. Therefore, you cannot create a subclass from a class declared as final. You can however instantiate a final class. Example is given in Listing 1.2

public final class Circle {
	private int height;	
	public Circle() { //constructor
	}	
}

public class newCircle extends Circle { //gives error
	
}

Listing 1.2: Final class

 

I recommend you run the code in Listing 1.2. Notice the error message. You cannot subclass a final class.

 

3. Abstract Modifier (abstract keyword)

You use abstract modifier for classes and method. Cannot be used for variables.

 

(a) Abstract Class

You cannot instantiate an abstract class. This means you cannot create objects from abstract class. What then can you do with it? It exists only for creating subclasses. Therefore you extend an abstract class.

It then follows that a class cannot be both final and abstract.

If you have an abstract method in a class, then that class must be abstract. However, you can have an abstract class without any abstract method.

Two examples of abstract classes are given in Listing 1.3.

//An abtract class with both abstract and non-abstract methods
public abstract class Triangle {
	public int height;	

	public void Draw(int height) {  //Non-abstract method
		
	}
	
	public abstract void Draw();  //Abstract method
	
}

//An abstract class with no abstract method
public abstract class Square {
	public int side;	

	public void Draw(int side) {  //Non-abstract method
		
	}	
}

Listing 1.3: Abstract Classes

 

Attempt these codes yourself. Then you will gradually get it very clear. If you have challenges, let me know in the comment below. I would give you the needed support

Furthermore, you cannot have a method body in an abstract method.

 

4. Synchronized Modifier (synchronized keyword)

You use this with applications the use threading. It tells the compiler that the method can only be accessed by one thread at a time. Moreover, you can combine synchronize with any of the four access level modifiers.

Example is given below

	public synchronized void Display() {
		//codes
	}

 

5. Volatile Modifier (volatile keyword)

Applies to variables. You use volatile to indicate that a thread accessing the variable must always merge its own private copy of this variable with the master copy that is in memory.

Therefore when a volatile variable is accessed, all cached copy of the variable in main memory is synchronized

 

6. Transient Modifier (transient keyword)

This applies to variables. This modifier tells Java that the variable should be skipped when the object is being serialized.