Java non-access modifier

Java provides many non-access modifiers to achieve many other functions.
  • static Modifiers are used to create class methods and variables.
  • final Modifiers are used to complete the implementation of classes, methods and variables.
  • abstract Modifiers are used to create abstract classes and methods.
  • synchronized and volatile modifiers, for threads.

Let's understand and learn these non-access modifiers one by one.

1. static modifier

1.1. Static variables

static keywords are used to create variables independent of class instances. Regardless of the number of instances of the class, there is only one copy of the static variable. Static variables are also known as class variables. Local variables cannot be declared as static.

1.2. Static methods

static Keywords are used to create methods independent of class instances.
Static methods cannot use instance variables that are objects of the class, and static methods are also called class methods. Static methods take all data from parameters and calculate something from those parameters without referencing variables. A class variable or method can be accessed using the class name followed by a dot ( .) and the name of the variable or method.

example

  • static Modifiers are used to create class methods and variables as shown in the following example −
public class InstanceCounter {

   private static int numInstances = 0;

   protected static int getCount() {
      return numInstances;
   }

   private static void addInstance() {
      numInstances++;
   }

   InstanceCounter() {
      InstanceCounter.addInstance();
   }

   public static void main(String[] arguments) {
      System.out.println("Starting with " + InstanceCounter.getCount() + " instances");

      for (int i = 0; i < 500; ++i) {
         new InstanceCounter();
      }
      System.out.println("Created " + InstanceCounter.getCount() + " instances");
   }
}

Execute the above sample code and get the following results:

Started with 0 instances
Created 500 instances

 

2. final modifier

2.1. final variable

A final variable can only be explicitly initialized once, and finala reference variable declared as final can never be reassigned to refer to a different object. However, the data within the object can be changed. Therefore, the state of the object can be changed, but not the reference.
For variables, the finalmodifier is usually staticused with to make the constant a class variable.

example

public class Test {
   final int value = 10;

   // 以下是声明常量的示例:
   public static final int BOXWIDTH = 6;
   static final String TITLE = "Manager";

   public void changeValue() {
      value = 12;   // 会出错,不能重新赋值
   }
}

2.2. final method

No subclass can override the final method. As mentioned earlier, final decorators prevent methods from being modified in subclasses.

The main purpose of declaring a finalmethod is to prevent others from changing the contents of the method.

Example
Methods can be declared using finalmodifiers in the class declaration as shown in the following example −

public class Test {
   public final void changeName() {
      
   }
}
Java

2.3. final class

The main purpose of using a class declared as finalis to prevent the class from being sub classed. If a class is marked as final, then this class cannot be inherited by other classes.

example

public final class Test {
   // body of class
}
 

3. abstract modifier

3.1. Abstract classes

Abstract ( abstract) classes cannot be instantiated. If a class is declared abstract ( abstract), then the only purpose is to extend that class.

A class cannot be both abstract and final(because finalclasses cannot be extended). If a class contains abstract methods, then the class should be declared as such abstract. Otherwise, a compilation error will be thrown.

An abstract class can contain abstract methods as well as ordinary methods.

example

abstract class Caravan {
   private double price;
   private String model;
   private String year;
   public void getYear(String y){};
   public abstract void goFast();   
   public abstract void changeColor();
}
Java

3.2. Abstract methods
An abstract method is a method declared without any implementation. The method body (implementation) is provided by the subclass. Abstract methods are never final or strict.

Any class that extends an abstract class must implement all the abstract methods of the superclass, unless the subclass is also an abstract class.

If a class contains one or more abstract methods, the class must be declared abstract. Abstract classes do not need to contain abstract methods.

Abstract methods end with a semicolon. Example: public abstract sample();

example

public abstract class SuperClass {
   abstract void m();   
}

class SubClass extends SuperClass {
   
   void m() {
      
   }
}
 

4. The synchronized modifier

synchronized Keywords are used to indicate that only one method can be accessed at a time. synchronized Modifiers can be applied to any of the four access-level modifiers.

example

public synchronized void showDetails() {
   .......
}

5. transient modifier

An instance variable is marked with transient, which indicates that the JVM skips the particular variable when serializing the object containing it.

This modifier is included in the statement that creates the variable, before the variable's class or data type.

example

public transient int limit = 55;   // will not persist
public int b;   // will persist
 

 

6. The volatile modifier

volatile The modifier is used to let the JVM know that a thread accessing a variable must always merge its own private copy of the variable with the main copy in memory.

Accessing a volatile variable synchronizes all cached copies of the variable in main memory. volatile Can only be applied to instance variables of type private. volatile Object references can be null.

example

public class MyRunnable implements Runnable {
   private volatile boolean active;

   public void run() {
      active = true;
      while (active) {   // line 1
         // some code here
      }
   }

   public void stop() {
      active = false;   // line 2
   }
}
Java

Normally, called in one thread ( Runnable the thread where it was started with) and called run()from another thread stop(). The loop may not stop when the cached value 1is used in line 1. active activefalse