Various modifiers and access modifiers in Java
Class:
Access modifier modifier Class class name extends parent class name Implement Interface name
(The location of the access modifier and the modifier can be interchanged)
Access modifiers |
Name |
Description |
Note |
Public |
can be accessed by all classes (using) |
The public class must be defined in a file with the same name as the class name |
Package |
can be accessed by a class in the same package (using) |
The default access rights, which can be omitted from this keyword, can be defined in the same file as the public class |
Modifier |
Name |
Description |
Note |
Final |
Classes that use this modifier cannot be inherited |
|
Abstract |
If you want to use the abstract class, you must first build a new class that inherits the abstract class, and the new class implements the abstraction method in the abstract class. |
class must be defined as abstract, as long as there is an abstract method, but the abstract class is not necessarily protected by the abstract method |
Variable
There are no global variables in Java, only method variables, instance variables (non-static variables in a class), class variables (static variables in a class).
The variables in the L method cannot have access modifiers. So the following access modifiers are only for variables defined in the class.
L When declaring an instance variable, if no initial value is assigned, it will be initialized to null (reference type) or 0, False (original type).
The more complex instance variables can be initialized by an instance variable initializer, which is a block of statements with {}, which runs after the constructor of the class is invoked, before the constructor of the parent class constructor.
The L class variable (static variable) can also be initialized by a class-variable initializer, which is a block of statements contained in static{} and may only be initialized once.
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 access the |
Modifier |
Name |
Description |
Note |
Static |
Static variables (also called class variables, others are called instance variables) |
Can be shared by all instances of the class. You do not need to create an instance of the class to access the static variable |
Final |
Constant, the value can only be assigned once and cannot be changed |
Be careful 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 |
Transient |
Tells the compiler that this variable does not need to persist when the class object is serialized |
Mainly because the amount of change can be obtained by other variables, it is used for performance problems |
Volatile |
Indicates that there may be multiple threads modifying this variable, requiring compiler optimizations to ensure that modifications to this variable are handled correctly |
|
Method
Access modifier modifier return type method name (parameter list) throws violation list
The constructor method of the L class can not have modifiers, return types, and throws clauses
When the constructor method of the L class is called, it first calls the constructor method of the parent class, and then runs the initializer for the instance variable and the static variable before the constructor itself is run.
L If the constructor method does not display a constructor that calls a parent class, the compiler automatically adds a default super () to it, and if the parent does not have a default parameterless constructor, the compiler will error. Super must be the first clause of a constructor method.
L NOTE the use of the private constructor method.
Access modifiers |
Name |
Description |
Note |
Public |
can be accessed from all classes |
|
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 access the |
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 |
Prevent any subclasses from overloading the method |
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
Access modifier Interface Interface Name extends interface list
The L interface is not able to define any implementations of its declared methods
Variables in the L interface always need to be defined as "public static final interface name", but can not contain these modifiers, the compiler defaults to this, the display contains modifiers mainly for the program clear
Access modifiers |
Name |
Description |
Public |
All |
No access modifier (default) |
In the same package |
Http://itlab.idcquan.com/Java/base/793981.html
Various modifiers and access modifiers in Java