What modifiers are there for classes, properties, and methods in the Java language? Briefly describe the difference between modifiers

1. Class modifiers are divided into two types: accessible control and non-access control.

Access control specifiers are: public class modifier public

Non-access controllers are: abstract class modifier abstract; final class modifier final

      1. Public class modifier public: There is only one accessible controller for a class in the Java language: public is public. The main class of every Java program must be a public class as a public utility. Those used by other classes and programs should be defined as public classes.

      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 the abstract class of the Java language.

      3. Final class modifier final: When a class cannot have subclasses, the modifier final can be used to describe it as a final class. A class defined as final is usually a class that has a fixed role and is used to perform some standard function.

      4. 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 point, the class can only be accessed or referenced by classes in the same package. This access feature is also known as package accessibility.

 

 

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

There are 4 types of access controllers: public access controller: public ; private access controller: private ; protected access controller: protected ; private protected access controller: private protected

There are 4 types of non-access control specifiers: static domain modifier: static ; final domain modifier: final ; volatile (shared) domain modifier: volatile ; transient domain modifier: transient

      1. Public access controller public: The domain modified with public is called the 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 the security of operation and the encapsulation of data, the use of public fields should generally be reduced.

      2. Private access controller private: The member variable (domain) modified with private can only be accessed by the class itself, but cannot be referenced by any other class (including subclasses).

      3. Protected access controller protected: A member variable modified with protected can be referenced by three types: ① the class itself; ② other classes in the same package as it; ③ subclasses of this class in other packages. The main effect of using the modifier protected is to allow its subclasses in other packages to access specific properties of the superclass.

      4. Private protection access controller private protected: The member variable modified with the modifier private protected can be accessed and referenced by the class itself or the subclass of the class.

      5. Static domain modifier static: The member variable modified with static belongs only 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 stored in the class. The memory range of an 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 the field (member variable) of a class is declared by the modifier final, its value remains 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, pay attention to the change of the value of this member variable in use. Usually volatile is used to decorate fields that accept external input.

      8. Temporary domain modifier transient: The temporary domain modifier transient is used to define a temporary variable. Its characteristics are: a temporary variable defined 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 controllers: public access controller: public ; private access controller: private ; protected access controller: protected ; private protected access controller: private protected

There are 5 kinds of non-access controllers: abstract method controller: abstract; static method controller: static; final method controller: final; local method controller: native; synchronized method controller: synchronized

      1. Abstract method controller abstract: A method modified with the modifier abstract is called an abstract method. An abstract method is a method with only a method header, no method body and no operation implementation.

      2. 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 qualification 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 handle member variables belonging to the entire class, that is, static methods can only handle static fields.

      3. Final method controller final: A method modified with the modifier final is called a final method. A final method is a method whose functions and inner statements cannot be changed, i.e. a final method cannot be overloaded. In this way, the function and operation of this method are fixed, preventing the wrong definition of the key method of the parent class by the subclass of the current class, and ensuring the safety and correctness of the program. All methods qualified as private by the private modifier, as well as all methods contained in a final class (final class), are considered final.

      4. Local method controller native: The 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. Synchronization method controller synchronized: This modifier is mainly used for coordination and synchronization in programs that coexist with multiple threads.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325649940&siteId=291194637