There are modifiers that you can use to learn the basics of Java, which are decorated differently and have different scopes, so that you can use them correctly only if you comb it out. scopes for different modifiers to decorate content Public
Public, which means that the property (or method) is exposed and can be invoked directly by all users. Default Defaults
Default, which is not only accessed by this class, but is also visible in the same package. Private
Private, which means that the property (method) is accessible only within this class (visible within the class). (You want to use private and set and get methods for other methods to call, so that you can access the property in a uniform manner, and easy to maintain access rights and property data legitimacy).
If there is no special case, the property must be private and the method should be exposed publicly.
This is the default if you do not indicate who called the method. Be sure to write this when distinguishing between instance variables and local variables. protected
Protected protected, for subclasses and classes under the same package is visible, is public, free to use, without any restrictions, and for other external class,protected become private. The list contrasts as follows:
A common modifier modifies the scope of the content
Modifiers |
This class |
Under the same package (no matter what) |
Sub Class |
Other Packages |
Public |
✔ |
✔ |
✔ |
✔ |
Protected |
✔ |
✔ |
✔ |
|
Defaults (default) |
✔ |
✔ |
|
|
Private |
✔ |
|
|
|
What can be decorated with different modifiers Public , private, protected, and default defaults
These can be used to modify the member variables, member methods and construction methods, but some common, some uncommon, use the occasion of the conventional. In addition, public can be used to modify the class itself.
The following is an introduction to some special modifiers. Abstract
Abstract abstraction, you can modify the class, the method.
Abstract class:
An abstract-decorated class, called an abstraction class, cannot be instantiated, must be inherited, and if a class inherits an abstract class, the class is either itself an abstract class, or the class overrides all the abstract methods of the parent class.
Abstract can extract the commonality of subclasses to the maximum, and put them in the parent class to improve the simplicity of the program.
Abstract method:
Method has no method body and is decorated with abstract. A class with an abstract method must be an abstract class (or interface), and there is not necessarily an abstract method in an abstract class.
Note: You cannot coexist with final,private and static. Static
Static is a modifier that modifies a member (including member variables and member methods). The decorated members are shared by all objects.
The modified members have the following characteristics:
The ① is loaded as the class is loaded.
② takes precedence over object existence.
The ③ is shared by all objects.
④ can be invoked directly by the class name.
The use of static modifier variables can refer to the Java basics--the difference between member variables, local variables, and static variables final
Final Finally, it can modify the class, modify the member variable, and modify the member method.
The class that it modifies cannot be inherited.
The Member method that it modifies is not overridden by a quilt class.
The member variable it modifies is actually a constant.
For an abstract and final usage, you can refer to another blog post, "Java basics--the relationship between abstract classes and interfaces," as shown in the following list:
Cosmetic content for common modifiers
modifiers \ element |
Class itself |
Member variable |
Member Methods |
Construction method |
Public |
✔ |
✔ |
✔ |
✔ |
Default |
✔ |
✔ |
✔ |
✔ |
Private |
|
✔ |
✔ |
✔ |
Protected |
|
✔ |
✔ |
✔ |
Abstract |
✔ |
|
✔ |
|
Static |
|
✔ |
✔ |
|
Final |
✔ |
✔ |
✔ |
|
After the analysis can be seen that the same content can be modified by different modifiers, it is necessary to find common rules, as follows:
All classes are decorated with public and, under a Java file, we write only one class.
All member variables are decorated with private.
All member methods are decorated with public,
If you are decorating with public abstract in an abstract class or interface.
All construction methods are decorated with public,
If the class is a tool class or a single instance class, the construct is decorated with private.
Summary
Modifier Although many, but basically each other, there will be synergy, the same will have antagonistic effect. In different operations, the use of different modifiers, the program can be efficient and orderly operation.