public/protected/private
public means public, private means private, protected means protection, and writing nothing means default.
method:
public protected default private
√ √ √ √ √ In the same category
√ √ √ × In the same pack
√ √ × × in subclass
√ × × × Different packages
Member variables:
Considering the security and robustness issues, member variables are generally private, and some public methods are provided to modify member variables, and the input is judged and controlled in the modified method.
Private member variables can only be used inside the class. Public member variables can be used outside the class.
kind:
There are only two types of permission modifiers for the class that are not added and public. Without adding a class that only the same package can access, all other classes of the public class can access.
Static
Means static, used to modify member variables, methods, and code blocks.
Static member variables
Static member variables are loaded as the class is loaded, and multiple objects share the static member variable. Look at the following code:
Static method
When modifying the method, this method is loaded with the loading of the class and is called directly through the class name. Many methods in tool classes are static and are called directly through "class name. method name". However, non-static member variables and non-static methods cannot be used in static methods.
Static code blocks
Static code blocks (Java classes will also call constructor blocks when they generate objects).
Please see the following code (prove the order of operation of constructor methods, constructor statements, and static constructor statements):
What should be noted is:
(1) Static methods cannot call non-static methods. Static methods are loaded when constructing the class. The static methods will be loaded when the class is loaded. At this time, the non-static methods have not been loaded, and the non-static methods will be loaded after the class is initialized. Therefore, the static methods cannot call non-static methods. It is perfectly possible to access indirectly by creating objects (or objects passed in formal parameters) inside a static method.
(2) The main method in java must be written static, because the object cannot be created when the class is loaded, and the static method can not be called through the object. Therefore, when the class is loaded, you can run the program through the main method entry.
Note: The parent class is a static method, and the child class cannot be overridden as a non-static method. On the premise of complying with the coverage rules, in the parent and child class, the static methods in the parent class can be overwritten by the static methods in the subclass, but there is no polymorphism.
When is a class loaded? Time (delayed loading, no loading if it can be loaded
(1) When new object is loaded
This is easy to understand, and I won't explain it anymore. (2) No object is created, static members (methods and properties) in the class are accessed
(3) Declare a reference to a class, do not load (4) Create a subclass, first load the parent class, then load the child class.
(5) Public static method in the parent class, subclass inheritance, call this method using the subclass class name, and load the parent class
Abstract
This modifier represents abstraction and can be used before methods and classes.
Abstract method indicates that the class should have this function, but different subclasses should have different implementation methods, so there is no definition of how to do it. How to implement it yourself by subclasses. Subclasses can only new objects after implementing all abstract methods.
If there are abstract methods inside a class, the class must also be abstract. Abstract classes cannot directly new objects. There can be non-abstract methods in abstract classes.
The above is the full content of the editor’s summary of modifiers in Java (except for Fina). I hope it will be helpful to everyone and support Wulin.com more~