Available modifiers for classes, methods, member variables, and local variables
| Modifier |
Class |
Member Methods |
Construction method |
Member variables |
Local variables |
| Abstract |
Y |
Y |
_ |
_ |
_ |
| Static |
_ |
Y |
_ |
Y |
_ |
| Public |
Y |
Y |
Y |
Y |
_ |
| Protected |
_ |
Y |
Y |
Y |
_ |
| Private |
_ |
Y |
Y |
Y |
_ |
| Synchronized |
_ |
Y |
_ |
_ |
_ |
| Native |
_ |
Y |
_ |
_ |
_ |
| Transient |
_ |
_ |
_ |
Y |
_ |
| Volatile |
_ |
_ |
_ |
Y |
_ |
| Final |
Y |
Y |
_ |
Y |
Y |
1. Access control modifier:The Java language uses access control modifiers to control access to methods and variables for classes and classes, exposing interfaces only to the consumer, but hiding implementation details.
- Public level: With public decoration, open to the outside
- Protected level: exposed to subclasses and classes in the same package with protected adornments
- Default level: No access control modifiers are exposed to classes in the same package
- Private level: Decorated with private, only the class itself can be accessed, not publicly
the top-level class has only the default or public level, so it cannot be decorated with private and protected. 2. Abstract modifier:used to modify classes and member methods, abstract classes cannot be instantiated, and abstract methods do not provide specific implementations.
- Abstract classes can have no abstract methods, but classes that contain abstract methods must be defined as abstract classes
- If a subclass does not implement all of the abstract methods in the parent class, then the subclass must also be defined as an abstract class
- No abstract construction method, no abstract static method
- Abstract classes can have non-abstract construction methods that may be called when an instance of a subclass is created
- Abstract classes and abstract methods cannot be modified by the final modifier (why?). )
3. Final modifier:
- With "immutable" meaning, it can modify non-abstract class fly, non-abstract member methods and variables.
- Class with final decoration cannot be inherited, no subclasses
- The method of final modification cannot overwrite the methods of the quilt class
- A constant is represented by a final modified variable and can only be assigned one time value
- Final cannot be used to modify the construction method, meaningless (why?) )
Final class:
- Classes not specifically designed for inheritance
- The implementation details of the class are not allowed to change for security reasons
- When creating the object model, be sure that the class will no longer be extended
Final method:
- For security reasons, the parent class does not allow subclasses to overwrite a method, and this method can be declared as final
final variable characteristics:
- You can modify static variables, instance variables, and local variables, representing static constants, instance constants, and local constants, respectively.
- The final variable must show initialization
- Final variable can only be assigned one time value
- If a variable of a reference type is final decorated, the variable can only always reference only one object, but it may change the content
final defines constants that have the following effects:
- Improve program security and prohibit illegal modification of data that is fixed and not allowed to change
- Improved maintainability of program code
- Improve the readability of program code
4. Static modifier:
- Static variables are represented by statically modified variables, which can be accessed directly from the class name.
- A static method is represented by a statically decorated member method, which can be accessed directly from the class name.
- A static code block is represented by a statically decorated program code block, which executes the code when the Java Virtual machine loads the class.
static variable:
-
Static variables have only one copy in memory
-
Allocates memory once for each instance of an instance variable, and does not affect
-
Static variables can be shared by all instances of the class and can be used as shared data for communication between instances
static method (why)
- The This keyword cannot be used in a static method, nor does it directly access the instance variables and instance methods of the owning class
- You can directly access the static variables and static methods of the owning class
- The Super keyword is also not available in static methods
- A static method cannot be defined as an abstract method and must be implemented
Static code block
- A class can also contain a static block of code that does not exist in any method body.
- These static blocks of code are executed when the Java Virtual machine loads the class. The constructor of a class is used to initialize an instance of the class, whereas a static code block of a class can be used to initialize the class.
- Static code blocks, like static methods, do not directly access instance variables and instance methods of a class, but must be accessed through the instance's reference
Modifiers in the Java language