1. Class modifiers are divided into two types: accessible control characters and non-access control characters.
Accessible control symbols are: public class modifier public
Non-access control symbols are: abstract class modifier abstract; final class modifier final
1) Public class Modifier public: There is only one access control modifier for a class in the Java language: public, which is public. The main class of every Java program must be a public class. As a public tool for use by other classes and programs, it should be defined as a public class.
2) Abstract class modifier abstract: Any class modified with the abstract modifier is called an abstract class. The so-called abstract class refers to a conceptual class that has no concrete objects. Such a class is an abstract class of the Java language.
3) Final class modifier final: When a class cannot have subclasses, the modifier final can be used to indicate it as a final class. Classes defined as final are usually classes that have a fixed role and are used to complete certain standard functions.
4) Class default access control character: If a class does not have an 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.
2 . Domain control modifiers are also divided into two categories: accessible control characters and non-access control characters.
There are 4 types of access control characters: public access control character: public; private access control character: private; protected access control character: protected; private protection access control character: private protected
non There are 4 types of access control modifiers: static domain modifier: static; final domain modifier: final; volatile (shared) domain modifier: volatile; temporary domain modifier: transient
1) Public access control Symbol public: The domain modified with public is called a public domain. If a public field belongs to a public class, it can be referenced by all other classes. Since the public modifier will reduce operational security and data encapsulation, the use of public fields should generally be reduced.
2) Private access control character private: Member variables (domains) modified with private can only be accessed by the class itself and cannot be referenced by any other class (including subclasses).
3) Protected access control character protected: Member variables modified with protected can be referenced by three categories: ① the class itself; ② other classes in the same package as it; ③ in other packages Subclasses of this class. The main purpose of using the protected modifier is to allow its subclasses in other packages to access specific properties of the parent class.
4) Private protected access control character private protected: Member variables modified with the modifier private protected can be accessed and referenced by the class itself or subclasses of the class.
5) Static domain modifier static: Member variables modified with static only belong to the variables of the class, and do not belong to any specific object. The value of the static member variable is a public storage stored in the memory area of the class. unit, rather than being stored in the memory range of a certain object. When an object of any class accesses it, it gets the same data; when an object of any class modifies it, it also operates on the same memory unit.
6) Final domain modifier final: The final domain modifier final is used to define symbolic constants. If a field (member variable) of a class is specified by the modifier final, its value will remain unchanged throughout the execution of the program.
7) Volatile (shared) domain modifier volatile: 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 by other programs or change its value. Therefore, you should pay attention to the changes in the value of this member variable during use. Usually volatile is used to modify fields that accept external input.
8) Temporary domain modifier transient: The transient domain modifier transient is used to define a temporary variable. Its characteristics are: a temporary variable qualified with the modifier transient will specify the Java virtual machine to determine that the temporary variable does not belong to a permanent state, so as to realize the archiving function of different objects. Otherwise, all variables in the class are part of the object's permanent state and must be saved when the object is stored.
3. The control modifiers of methods are also divided into two categories: accessible control characters and non-access control characters.
There are 4 types of access control characters: public access control character: public; private access control character: private; protected access control character: protected; private protection access control character: private protected
non There are 5 types of access control characters: abstract method control character: abstract; static method control character: static; final method control character: final; local method control character: native; synchronized method control character: synchronized
1) Abstract Method control operator abstract: A method modified with the abstract modifier is called an abstract method. An abstract method is a method with only a method header and no method body and operation implementation.
2) Static method control character static: A method modified with the modifier static is called a static method. Static methods are class methods that belong to the entire class; methods that are not modified or qualified with static are methods that belong 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 the static domain.
3) Final method control character final: A method modified with the modifier final is called a final method. A final method is a method whose functionality and internal statements cannot be changed, i.e. a final method cannot be overloaded. In this way, the functions and operations of this method are fixed, preventing subclasses of the current class from incorrectly defining key methods of the parent class, and ensuring the safety and correctness of the program. All methods qualified as private by the private modifier, and all methods contained in a final class, are considered final methods.
4) Local method control character native: A method modified with the modifier native is called a local method. In order to improve the running speed of the program, the method body of the program needs to be written in other high-level languages. Then the method can be defined as a local method and modified with the modifier native;
5) Synchronized method control symbol synchronized: This modification Symbols are mainly used for coordination and synchronization in multi-threaded programs.
For more detailed analysis of access modifiers in Java and related articles, please pay attention to the PHP Chinese website!