Review the past and learn the new: Various modifiers in Java

The following content of this article is based on JDK 8 version.

1. Introduction

Java modifiers are divided into class modifiers, method modifiers, and variable modifiers according to the modified objects. Modifiers are divided into access control modifiers and non-access control modifiers .

2. Class modifier

2.1. Access modifier

Public class modifier public: The only access control modifier for a class in the Java language is public, that is, public. Each Java program file has one and only one class that is public, which is called the main class. Other external classes have no access control modifier, that is, the default modifier. They have package access and cannot be accessed by other packages. Note: The inner class of a class can be modified by other access control modifiers protected, default (default, friendly), and private, which is equivalent to a member of the class.

Note: If a Java class or property has a default access control modifier, it belongs to the default/friendly type modifier, but in fact there is no access modifier named default or friendly in Java (that is, you cannot use default or friendly to define classes or variables ), just to facilitate the identification of the default access control character.

2.2, non-access control character

a. Abstract class modifier abstract: A class modified with abstract modifier is called an abstract class.

b. Final class modifier final: When a class cannot be inherited, the modifier final can be used to modify it into a final class. Classes defined as final are usually classes that have a fixed role and are used to complete certain standard functions.

c. Class default access control character: If a class has no access control character, it means that it has the default access control character. At this time, this class can only be accessed or referenced by classes in the same package. This access feature is also called package accessibility.

3. Method modifier

3.1, access control modifier

The public access control character public, the protection access control character protected, the default default access control character, and the private access control character private.

3.2, non-access control modifier

Abstract method controller abstract , static method controller static , final method controller final, local method controller native >, Synchronization method control charactersynchronized.

a. Abstract method control symbol abstract: The method modified by abstract is called an abstract method. An abstract method has only a method header, without a method body and no concrete implementation.

b. Static method controller static: A method modified with the modifier static is called a static method. A static method is a class method that belongs to the entire class; a method that does not use static modification and limitation is a method that belongs to a specific class object. Since the static method belongs to the entire class, it cannot manipulate and process member variables belonging to an object, but can only process member variables belonging to the entire class, that is, the static method can only process static fields.

c. The final method controller final: The method modified with the modifier final is called the final method. A final method is a method whose functions and internal statements cannot be changed, that is, a final method cannot be overridden. final fixes the functions and operations of the method, prevents the subclass of the current class from misdefining the key methods of the parent class, and ensures the safety and correctness of the program. All methods that are made private by the private modifier, and all methods contained in a final class (final class), are considered final.

d. Local method control character native: The method modified with the modifier native is called a local method. In order to improve the running speed of the program, it is necessary to write the method body of the program in other high-level languages, then the method can be defined as a local method and modified with the modifier native.

e. Synchronization method controller synchronized: This modifier is mainly used for coordination and synchronization in multi-threaded programs.

4. Variable modifier

4.1, access control character

Public access control character public , protected access control character protected, default default access control character, private access control character private.

a. Public access control character public: The domain modified with public is called the public domain. Since the public modifier will reduce the security of operation and the encapsulation of data, the use of public fields should generally be reduced.

b. Private access control character private: The member variable modified by private can only be accessed by the class itself, and cannot be accessed by any other class (including subclasses).

b. Protected access control symbol protected: member variables modified with protected can be referenced by three types: ① the class itself; ② other classes in the same package; ③ subclasses in other packages. The main function of using the modifier protected is to allow subclasses in other packages to access specific properties of the parent class.

d. Default default modifier: There is no access control character or member variables modified with default can be accessed by the class itself or other classes in the same package.

4.2, non-access control characters

static field modifier static , final field modifier final , volatile (shared) field modifier volatile , transient field modifier >transient.

a. Static domain modifier static: The member variable modified with static only belongs to the variable of the class, not to any specific object. The value of the static member variable is stored in the public storage unit of the memory area of the class, not in the The memory range of a certain object. When any object of this class accesses it, it gets the same data; when any object of this class modifies it, it also operates on the same memory unit.

b. The final field modifier final: The final field modifier final is used to define constants. If a field (member variable) of a class is declared by the modifier final, its value will remain unchanged throughout the execution of the program.

c. Volatile (shared) domain modifier volatile: The volatile (shared) domain modifier volatile is used to indicate that this member variable may be controlled and modified by several threads. That is to say, during the running of the program, this member variable may be affected or change its value by other programs. Usually volatile is used to decorate fields that accept external input.

d. Temporary domain modifier transient: The temporary domain modifier transient is used to define a temporary variable. Its characteristics are: the temporary variable limited by the modifier transient will specify that the Java virtual machine considers the temporary variable as not belonging to the permanent state, so as to realize the archiving function of different objects. Otherwise, all variables in the class are part of the permanent state of the object and must be saved when storing the object.

5. Summary of access control modifiers

Access Level Access Control Modifier Same Same Package Child class (different package) different package (other class)
public public allowed Allow Allow Allow
Protected protected Allow Allow Allow Disallow
Default Default Modifier Allow Allow Disallow Disallow
Private private Allowed Not Allowed Not Allowed Not Allowed Not Allowed

Note:Protects or methods modified by protected allow access in subclasses of different packages. Note that the access method here is to access the protected property or method in the parent class through inheritance, rather than directly accessing the protected property or method through the parent class instance.

Note: When the subclass and the parent class are in the same package, access control is controlled according to the scope of the same package.

Test class:

package basic.modifier;

public class Demo {

// public>>protected>>default>>private
    public String publicAage = "publicAage";
    protected String protectedAage = "protectedAage";
    String defaultAage = "defaultAage";
    private String privateAage = "privateAage";

    public void publicSay(){
        System.out.println("Output the method of the parent class publicSay");
    }

    protected void protectedSay(){
        System.out.println("Output the method of parent class protectedSay");
    }

    void defaultSay(){
        System.out.println("Output the method of parent class defaultSay");
    }

    private void privateSay(){
        System.out.println("Output the method privateSay of the parent class");
    }
}

reference article

Summary of the use of various modifiers in Java (reading this article is enough) – Zhihu