The Java language defines 6 commonly used modifiers, including public, protected, private, abstract, static and final, and 5 less commonly used modifiers. The following is an introduction to these 11 Java modifiers:
1.public
Use objects: classes, interfaces, members
Introduction: No matter where the package it is in is defined, the class (interface, member) is accessible
2.private
Use object: member
Introduction: member can only be accessed in the class in which it is defined
3.static
Use objects: classes, methods, fields, 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. A static field is a class field. No matter how many instances are created by the class in which the field is located, there is only one instance of the field that points to the class it belongs to rather than an instance of the class. The initialization function is executed when the class is loaded, not when the instance is created.
4.final
Used objects: classes, methods, fields, variables
Introduction: A class defined as final does not allow subclasses and cannot be overwritten (not Applied to dynamic queries), field values are not allowed to be modified.
5.abstract
Used 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
Usage 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
Used 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.strictfp
Use objects: classes, methods
Introduction: All methods in classes modified by strictfp hide the strictfp modifier, and all floating points executed by the method Calculations comply with the IEEE 754 standard. All values, including intermediate results, 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.synchronized
Use object: method
Introduction: For a static method, the jvm locks the class it is in before execution; for a non-static class Method, lock a specific object instance before executing
.
10.volatile
Using objects: fields
Introduction: Because asynchronous threads can access fields, some optimization operations must not be applied to fields. volatile can sometimes replace synchronized.
11.transient
Using objects: fields
Introduction: Fields are not part of the persistent state of the object, and fields should not be strung together with the object.
The above is the detailed content of What are the modifiers in Java?. For more information, please follow other related articles on the PHP Chinese website!