Detailed resolution of access modifiers in Java

  • 2020-04-01 02:17:40
  • OfStack

1, the class of modifiers are divided into: access control and non-access control characters.

The accessable control is the: public class modifier public

Non-access control characters are: abstract class modifier abstract; Final class modifier final

1) public modifier: There is only one accessable control for a class in the Java language: public. The main class of every Java program must be a public class for other classes and programs to use as a public utility should be defined as a public class.

Abstract class modifier abstract: Classes that are decorated with the abstract modifier are called abstract classes. An abstract class is a conceptual class that has no concrete object. Such a class is the Java language's abstract class.

3) final class modifier final: When a class cannot be subclassed, the modifier final can be used to declare it as the final class. Classes that are defined as final are usually classes that have a fixed purpose and are used to perform some standard function.

4) class default access control: If a class does not have an access controller, it has a default access controller property. At this point, the class can only be accessed or referenced by classes in the same package. This access feature is also known as package access.

2. The control modifiers of the domain are also divided into two categories: accessible control and non-access control.

There are four types of accessible controls: public access control: public; Private access control: private; Protected access control: protected; Private protected access control: private protected

There are four types of non-access controls: static field modifier: static; Final field modifier: final; Volatile (Shared) field modifier: volatile; Transient field modifier: transient

1) public access control: Fields that are decorated with public are called public domains. If the public domain belongs to a public class, it can be referenced by all other classes. Because the public modifier reduces the security of the run and encapsulation of the data, the use of the public domain should generally be reduced.

2) private access control character private: Member variables (fields) decorated with private can only be accessed by the class itself and cannot be referenced by any other class, including subclasses.

3) protect the access control character protected: A member variable decorated with protected can be referenced by three categories: (1) the class itself; With it in the same package in other classes; A subclass of this class in another package. The main use of the modifier protected is to allow subclasses of it in other packages to access specific properties of the parent class.

4) private protected access control character private protected: A member variable decorated with the modifier private protected can be accessed and referenced by both the class itself and its subclasses.

5) static field modifier static: Static member variables belong to class variables only, but not to any specific object. The values of static member variables are stored in the public storage unit in the memory area of the class, rather than in the memory interval of an object. Any object of a class that accesses it fetches the same data; When an object of any class modifies it, it also operates on the same memory unit.

6) final field modifier final: The final field modifier final is used to define symbolic constants. If the field (member variable) of a class is specified by the modifier final, its value remains constant throughout the execution of the program.

7) volatile (Shared) field modifier volatile: The volatile (Shared) field modifier volatile is used to indicate that the member variable may be controlled and modified by several threads. That is to say, in the process of program running, this member variable may be affected by other programs or change its value. Therefore, pay attention to the change of the value of this member variable in use. Volatile is often used to modify fields that accept external input.

8) transient field modifier: The transient field modifier transient is used to define a transient variable. Its characteristics are: transient variables qualified by the qualifier transient will be designated by the Java virtual machine to determine that the temporary variables do not belong to the permanent state, in order to achieve the archiving function of different objects. Otherwise, all variables in the class are part of the object's permanent state and must be stored together with the object.

3. Method control modifiers are also divided into two categories: accessible control and non-access control.

There are four types of accessible controls: public access control: public; Private access control: private; Protected access control: protected; Private protected access control: private protected

There are five types of non-access controllers: abstract method controller: abstract; Static method control: static; Final method control: final; Local method controller: native; Synchronized method controller: synchronized

Abstract method controller abstract: Methods that are decorated with the modifier abstract are called abstract methods. An abstract method is a method that has only the method header and no method body or operation implementation.

2) static method control character static: Methods that are decorated with the static modifier are called static methods. A static method is a class method that belongs to the entire class. Methods that are not static and qualified are methods that belong to a concrete class object. Since the static method belongs to the whole class, it cannot manipulate and handle the member variables belonging to an object, but can only handle the member variables belonging to the whole class, that is, the static method can only handle the static field.

3) final method control character final: Methods that modify with the modifier final are called final methods. A final method is a method whose functionality and internal statements cannot be changed, that is, the final method cannot be overloaded. In this way, the function and operation of this method are fixed, which prevents the subclass of the current class from misdefining the key methods of the parent class, and guarantees the security and correctness of the program. All methods that are qualified as private by the private modifier, as well as all methods contained in final classes (final classes), are considered final methods.

4) native: Methods that modify with the modifier native are called local methods. 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, so this method can be defined as the local method modified with the modifier native.

5) synchronized: This modifier is primarily used for coordination and synchronization in programs where multiple threads coexist.

Related articles: