One, Java modifier
The Java language provides a number of modifiers, mainly divided into the following two categories:
- Access modifiers
- Non-access modifiers
1. Access Control modifiers
In Java, you can use access controls to protect access to classes, variables, methods, and construction methods. JAVAV supports 4 different types of access rights.
-
default (That is, by default, nothing is written): it is visible within the same package, and no modifiers are used. Use objects: classes, interfaces, variables, methods.
-
Private : Visible within the same class. Working with objects: variables, methods. Note: Classes cannot be decorated (external classes)
-
Public: Visible to all classes. Working with objects: classes, interfaces, variables, methods
-
protected : Visible to classes and all subclasses within the same package. Working with objects: variables, methods. Note: Classes (external classes) cannot be decorated.
1.1. Access control and inheritance
Note the following methods inherit the rule:
-
Methods declared as public in the parent class must also be public in the subclass.
-
A method declared as protected in a parent class is either declared as protected in a subclass, or declared as public, and cannot be declared as private.
-
The method declared as private in the parent class cannot be inherited.
2. Non-access modifiers
Java also provides a number of non-access modifiers in order to implement some other functionality.
The static modifier, which is used to modify class methods and class variables.
Final modifiers, which are used to modify classes, methods, and variables, the final decorated class cannot be inherited, the decorated method cannot be redefined by the inheriting class, the modified variable is constant, and is not modifiable.
An abstract modifier that is used to create abstract classes and abstract methods.
Synchronized and volatile modifiers, which are used primarily for threading programming.
2.1. Static modifier
-
Static variables:
The static keyword is used to declare static variables that are independent of an object, regardless of how many objects a class instantiates, and its static variables have only one copy. Static variables are also known as class variables. A local variable cannot be declared as a static variable.
-
static method:
The static keyword is used to declare an object-independent method. Static methods cannot use non-static variables of a class. The static method obtains the data from the parameter list and then computes the data.
2.2. Final modifier
Final variable:
The final variable can be explicitly initialized and initialized only once. A reference to an object that is declared final cannot point to a different object. But the data in the final object can be changed. This means that the final object reference cannot be changed, but the value inside can be changed.
The final modifier is typically used with the static modifier to create a class constant.
Final method:
The final method in the class can inherit from the quilt class, but cannot be modified by the quilt class.
The primary purpose of declaring the final method is to prevent the content of the method from being modified.
Final class
The final class cannot be inherited, and no class can inherit any of the attributes of the final class.
2.3. Abstract modifier
Abstract class:
An abstract class cannot be used to instantiate an object, and the sole purpose of declaring an abstract class is to augment the class in the future.
A class cannot be both abstract and final decorated. If a class contains an abstract method, the class must be declared as an abstract class, or a compilation error will occur.
Abstract classes can contain both abstract and non-abstract methods.
Abstract methods
An abstract method is a method that does not have any implementation, and the specific implementation of the method is provided by the subclass.
Abstract methods cannot be declared final and static.
Any child class that inherits an abstract class must implement all the abstract methods of the parent class, unless the subclass is also an abstract class.
If a class contains several abstract methods, the class must be declared as an abstract class. Abstract classes can contain no abstract methods.
The declaration of an abstract method ends with a semicolon, for example: publicabstract sample ();.
2.4. Synchronized modifier
The method of synchronized keyword declaration can only be accessed by one thread at a time. The synchronized modifier can be applied to four access modifiers.
2.5. Transient modifier
When a serialized object contains an instance variable that is transient decorated, the Java Virtual Machine (JVM) skips that particular variable.
The modifier is included in the statement that defines the variable, which is used to preprocess the data type of the class and variable.
2.6. Volatile modifier
A volatile-modified member variable forces the value of the member variable to be re-read from shared memory each time it is accessed by the thread. Also, when a member variable changes, the thread is forced to write the value of the change back to the shared memory. So at any moment, two different threads always see the same value for a member variable.
A volatile object reference may be null.
Typically, you call the Run () method on one thread (the thread that is open on Runnable), and the Stop () method is called on another thread. If the active value of the buffer in the first row is used, the loop does not stop when the active value of the second row is false.
But in the code above we used the volatile modifier active, so the loop stops.
Java Learning Note (ii)---java modifier