Java modifiers are used to define classes, methods or variables, and are usually placed at the front of the statement. (Recommendation: java video tutorial)
Java language provides many modifiers, mainly divided into the following two categories:
Access modifier
non Access modifiers
The Java language defines 6 commonly used modifiers, including public, protected, private, abstract, static and final, and also defines 5 less commonly used modifiers. The following is a description of these Java modifiers. Introduction:
1. Public objects used: classes, interfaces, members Introduction: No matter where the package in which it is located is defined, the class (interface, member) is accessible.
2. Private usage object: Member introduction: Members can only be accessed in the class in which they are defined.
3. Static uses objects: classes, methods, fields, and initialization functions. Introduction: The inner class named static is a top-level class, which is not related to the members of the containing class. Static methods are class methods that are pointed to the class to which they belong rather than to instances of the class.
Static fields are class fields. No matter how many instances are created by the class in which the field is located, there is only one instance of this field that points to the class to which it belongs rather than an instance of the class. The initialization function is executed when the class is loaded, not when the instance is created.
4. Objects used by final: classes, methods, fields, variables Introduction: Classes defined as final do not allow subclasses, cannot be overwritten (should not be used for dynamic queries), and field values are not allowed to be modified. .
5. Abstract usage objects: classes, interfaces, methods Introduction: Classes include unimplemented methods and cannot be instantiated. If it is an abstract method, the method body is empty, the implementation of the method is defined in the subclass, and the class containing an abstract method must be an abstract class.
6. Protected use object: Member introduction: A member can only be accessed in the package in which it is defined. If it is accessed in other packages, the class that implements this method must be a subclass of the class to which the member belongs. .
7. Native usage object: Member introduction: It is related to the operating platform. Its method is not defined when it is defined. The implementation of the method is implemented by an external library.
8. Objects used by strictfp: Class, method introduction: All methods in the class modified by strictfp hide the strictfp modifier. All floating point calculations performed by the method comply with the IEEE754 standard, and all values include intermediate results. All must be represented as float or double types without taking advantage of the additional precision or representation range provided by the native platform floating point format or hardware.
9. Objects used by synchronized: Method introduction: For a static method, the jvm locks the class it is in before execution; for a non-static class method, a specific object instance is locked before execution.
10. Volatile usage objects: Field introduction: Because asynchronous threads can access fields, some optimization operations must not be applied to fields.
Volatile can sometimes replace synchronized.
11. Transient usage objects: Field introduction: Fields are not part of the persistent state of the object, and fields and objects should not be strung together.
For more java knowledge, please pay attention to the java basic tutorial column.
The above is the detailed content of What are java modifiers?. For more information, please follow other related articles on the PHP Chinese website!