Access modifiers
|
Access modifiers |
|
Name |
Description |
Note |
|
Public |
can be accessed by any class |
|
|
Protected |
can be accessed by all classes in the same package
can be accessed by all sub-classes |
Subclasses are not accessible in the same package |
|
Private |
can only be accessed by methods of the current class |
|
|
Default (no access modifier) |
can be accessed by all classes in the same package |
if the subclass is not in the same package, you cannot ask |
Variable modifiers
- Java has no global variables, only method variables, instance variables (non-static variables in the Class), class variables (static variables in the Class).
- , and the other two variables have the same access modifier as the previous table.
- When declaring an instance variable, if the initial value is not assigned, then . The
- can initialize more complex instance variables through an instance variable initializer. instance variable initialization block is a block of statements contained with {}, which runs after the constructor of the class is invoked, before the constructor is run after the parent class constructor. The
- class variable (static variable) can also be initialized with a class-variable initializer, which is a block of statements contained in static{} and may only be initialized once.
Mainly because the variable can come through other variables, use it for performance problems
|
modifier |
|
Name |
description |
remarks |
|
Static |
Static variables (also known as class variables, others called instance variables) |
can be shared by all instances of the class.
Does not need to create an instance of the class to access static variables |
|
Final |
constants, values can only be assigned once, cannot be changed |
Take care not to use const, although it is the same as the Const keyword in C, C + +, and can be used with static to avoid maintaining a copy of each instance of the class |
| P>transient
|
tells the compiler that when a class object serialization, this variable does not need to persist | TD Width= "348" >
|
Volatile |
< p> indicates that there may be multiple threads modifying this variable, requiring the compiler to optimize to ensure that modifications to this variable are handled correctly
|
|
- volatile: In the current Java memory model, threads can store variables in local memory (such as a machine's register), rather than read and write directly in main storage. This can cause a thread to modify the value of one variable in main memory, while another thread continues to use its copy of the variable value in the register, resulting in inconsistent data. Declaring a variable as volatile (unstable) will indicate that the JVM is unstable, and each time it is used it is read in main memory. Also, when a member variable changes, forcing the thread to write the change back to the shared memory. So at any moment, two different threads always see the same value for a member variable.
Method modifiers
- Access modifier + modifier + return type + method name (parameter list) + throws violation list
- Class constructor methods cannot have modifiers, return types, and throws clauses.
- When a class constructor method is called, it first constructs the parent class and then runs the initializer for the instance variable and the static variable, and then runs the constructor itself .
|
Modifier |
|
Name |
Description |
Note |
|
Static |
Static methods (also known as class methods, others called instance methods) |
Provide services that do not depend on class instances
You do not need to create an instance of a class to access a static method |
|
Final |
Prevents any subclasses from overriding the method (can be overloaded) |
Be careful not to use const, although it has the same meaning as the Const keyword in C, C + +
Can be used with static to avoid maintaining a copy of each instance of the class |
|
Abstract |
Abstract methods, methods that have been declared in a class and not implemented |
You cannot declare a static method, a final method, or a constructor method of a class as abstract |
|
Native |
The method defined with this modifier is not implemented in the class, and in most cases the implementation of the method is written in C and C + +. |
See Sun's Java Native Interface (JNI), which provides the runtime to load an implementation of a native method and associate it with a Java class function |
|
Synchronized |
Multi-threaded support |
When one of these methods is called, no other thread can call the method, and the other synchronized method cannot call the method until the method returns
|
Interface modifiers
- Access modifier Interface Interface Name extends interface list
-
|
Access modifiers |
|
Name |
Description |
|
Public |
All |
|
No access modifier (default) |
In the same package
|
<Java>< modifiers >