Java Modifiers
The Java language provides many modifiers, mainly divided into the following two categories:
- Access modifiers
- Non-access modifiers
Modifiers are used to define classes, methods, or variables, and are usually placed at the very beginning of a statement. We will illustrate this with the following example:
Access Control Modifiers
In Java, access control modifiers can be used to protect access to classes, variables, methods, and constructors. Java supports 4 different access levels.
-
default(i.e., default, nothing written): Visible within the same package, without using any modifier. Applies to: classes, interfaces, variables, methods.
-
private: Visible within the same class. Applies to: variables, methods.Note: Cannot modify classes (outer classes)
-
public: Visible to all classes. Applies to: classes, interfaces, variables, methods
-
protected: Visible to classes in the same package and all subclasses. Applies to: variables, methods.Note: Cannot modify classes (outer classes)。
We can illustrate the access levels with the following table:
| Modifier | Current class | Same package | Subclasses (same package) | Subclasses (different package) | Other packages |
|---|---|---|---|---|---|
public |
Y | Y | Y | Y | Y |
protected |
Y | Y | Y | Y/N(Description) | N |
default |
Y | Y | Y | N | N |
private |
Y | N | N | N | N |
Default Access Modifier - Without using any keyword
If no access modifier is specified in the definition of a class, variable, method, or constructor, they will by default have the default access modifier.
The access level of the default access modifier is package-level, meaning it can only be accessed by other classes in the same package.
As shown in the example below, variables and methods can be declared without using any modifier.
Example
In the above example, the MyClass class and its member variable x and method display() are all defined using the default access modifier. The MyOtherClass class is in the same package, so it can access the MyClass class and its member variables and methods.
Private Access Modifier - private
The private access modifier is the most restrictive access level, so methods, variables, and constructors declared asprivatemethods, variables, and constructors can only be accessed by the owning class, and classes and interfaces cannot be declared asprivate。
Variables declared as private access type can only be accessed by external classes through public getter methods in the class.
The use of the Private access modifier is mainly to hide the implementation details of the class and protect the data of the class.
The following class uses the private access modifier:
In the example, the format variable in the Logger class is a private variable, so other classes cannot directly get or set the value of the variable. In order to allow other classes to manipulate the variable, two public methods are defined: getFormat() (returns the value of format) and setFormat(String) (sets the value of format)
Public Access Modifier - public
Classes, methods, constructors, and interfaces declared as public can be accessed by any other class.
If several public classes that access each other are distributed in different packages, you need to import the package containing the corresponding public classes. Due to the inheritance of classes, all public methods and variables of a class can be inherited by its subclasses.
The following function uses public access control:
The main() method of a Java program must be set to public, otherwise the Java interpreter will not be able to run the class.
Protected Access Modifier - protected
protected needs to be analyzed and explained from the following two points:
Subclass and base class are in the same package:: Variables, methods, and constructors declared as protected can be accessed by any other class in the same package;
Subclass and base class are not in the same package:: Then in the subclass, a subclass instance can access the protected methods inherited from the base class, but cannot access the protected methods of a base class instance.
protected can modify data members, constructors, and method members.Cannot modify classes (except inner classes)。
Interfaces and their member variables and methods cannot be declared as protected. You can see the illustration below:

Subclasses can access methods and variables declared with the protected modifier, thus preventing unrelated classes from using these methods and variables.
The following parent class uses the protected access modifier, and the subclass overrides the parent class's openSpeaker() method.
If the openSpeaker() method is declared as private, then no class other than AudioPlayer will be able to access the method.
If openSpeaker() is declared as public, then all classes will be able to access the method.
If we only want the method to be visible to subclasses of its class, then declare the method as protected.
protected is one of the most difficult Java class member access modifiers to understand. For more details, please seeDetailed Explanation of Java protected Keyword。
Access Control and Inheritance
Please note the following rules for method inheritance:
Methods declared as public in the parent class must also be public in the subclass.
Methods declared as protected in the parent class must be declared as either protected or public in the subclass, and cannot be declared as private.
-
Methods declared as private in the parent class cannot be inherited by the subclass.
Non-Access Modifiers
In order to implement some other functions, Java also provides many non-access modifiers.
static modifier, used to modify class methods and class variables.
final modifier, used to modify classes, methods, and variables. Classes modified by final cannot be inherited, methods modified by final cannot be redefined by inherited classes, and variables modified by final are constants and cannot be modified.
abstract modifier, used to create abstract classes and abstract methods.
synchronized and volatile modifiers, mainly used in thread programming.
static Modifier
-
Static variables:
The static keyword is used to declare static variables that are independent of objects. No matter how many objects a class instantiates, its static variable has only one copy. Static variables are also called class variables. Local variables cannot be declared as static variables.
-
Static methods:
The static keyword is used to declare static methods that are independent of objects. Static methods cannot use non-static variables of the class. Static methods get data from the parameter list and then compute the data.
Access to class variables and methods can be done directly usingclassname.variablenameandclassname.methodnamethe method of access.
As shown in the example below, the static modifier is used to create class methods and class variables.
The output of running the above example is as follows:
Starting with 0 instances Created 500 instances
final Modifier
final variables:
final means "last" or "final". Once a variable is assigned a value, it cannot be reassigned. Instance variables modified by final must explicitly specify an initial value.
The final modifier is usually used together with the static modifier to create class constants.
Example
final methods
final methods in a parent class can be inherited by subclasses, but cannot be overridden by subclasses.
The main purpose of declaring a final method is to prevent the content of the method from being modified.
As shown below, declare a method using the final modifier.
final class
A final class cannot be inherited, and no class can inherit any characteristics of a final class.
Example
abstract Modifier
Abstract class:
An abstract class cannot be used to instantiate objects; the sole purpose of declaring an abstract class is to extend the class in the future.
A class cannot be modified by both abstract and final at the same time. If a class contains abstract methods, then the class must be declared as an abstract class, otherwise a compilation error will occur.
An abstract class can contain both abstract and non-abstract methods.
Example
Abstract method
An abstract method is a method without any implementation; the specific implementation is provided by subclasses.
Abstract methods cannot be declared as final or static.
Any subclass that inherits an abstract class must implement all abstract methods of the parent class, unless the subclass is also an abstract class.
If a class contains several abstract methods, then the class must be declared as an abstract class. An abstract class does not have to contain abstract methods.
An abstract method declaration ends with a semicolon, for example:public abstract sample();。
Example
synchronized Modifier
A method declared with the synchronized keyword can only be accessed by one thread at a time. The synchronized modifier can be applied to the four access modifiers.
Example
transient Modifier
When a serialized object contains instance variables modified by transient, the Java Virtual Machine (JVM) skips that particular variable.
This modifier is included in the statement defining the variable and is used to preprocess the data types of classes and variables.
Example
volatile Modifier
A member variable modified by volatile forces threads to re-read the value of that member variable from shared memory every time it is accessed. Moreover, when the member variable changes, threads are forced to write the changed value back to shared memory. In this way, at any moment, two different threads always see the same value for a given member variable.
A volatile object reference may be null.
Example
Normally, in one thread the run() method is called (in a thread started by Runnable), and in another thread the stop() method is called. IfFirst linethe active value in the buffer is used, thenSecond linewhen the active value is false, the loop will not stop.
But in the above code we used volatile to modify active, so the loop will stop.
Other extensions