Java learning-differences between public, private, protected, and other modifiers, privateprotected
Public, private, protecte, and default are used to define the access permissions of members in Java, that is, no permission modifier is added before the Members. For example:
Public class {
Void method (){};
}
Method is the default permission.
The access permissions of these four modifiers are as follows:
-----------------------------------------------
Class, package, subclass, and others
Public allow
Protected allowed not allowed
Allow by default or not
Private: Not allowed. Not allowed.
-----------------------------------------------
Description of the meanings of the access modifiers public, private, protecte, and default in Java:
Public (interface access permission): modifier with the widest access restriction in Java, which is generally called "public ". Classes, attributes, and Methods Modified by the package can be accessed not only across classes, but also across packages.
Private (you cannot access): the shortest modifier in Java, which is generally called "private ". The classes, attributes, and Methods Modified by the class can only be accessed by the objects of the class. The subclass of the class cannot be accessed, and cross-package access is not allowed.
Protected (inherited access permission): An access modifier between public and private. It is generally called a "protected form ". Classes, attributes, and Methods Modified by the class can only be accessed by methods and subclasses of the class, even if the subclass can be accessed in different packages.
Default (package access permission): it is usually called "default access mode" without any access modifier. In this mode, only access in the same package is allowed.
(Only in Java ......)
The Java language defines the six common modifiers public, protected, private, abstract, static, and final.
Five frequently-used modifiers are also defined. The following describes the 11 Java modifiers:
1. public
Objects used: classes, interfaces, and members
Introduction: No matter where the package is defined, this class (interface, member) is accessible.
2. private
Intended audience: Members
Introduction: A member can only be accessed in the class that defines it.
3. static
Object used: Class, method, field, initialization Function
Introduction: an internal class named static is a top-level class, which is irrelevant to the members of the class. Static Method
Is a class method,
Is to be directed to the class rather than the instance of the class. A static field is a class field.
Number of instances.
Only one instance is pointed to the class rather than the instance of the class. The initialization function is executed when the class is loaded.
Instead of creating
Executed when the instance is running.
4. final
Objects used: classes, methods, fields, and variables
Description: A class defined as final cannot contain child classes, cannot be overwritten (cannot be used for dynamic queries), and field values.
Not Allowed
Modify.
5. abstract
Objects used: classes, interfaces, and methods
Description: classes include methods that are not implemented and cannot be instantiated. If it is an abstract method, the method body
Null.
The implementation of the method is defined in the subclass, and the class that contains an abstract method must be an abstract class.
6. protected
Intended audience: Members
Introduction: A member can only be accessed in the defined package. If it is accessed in another package, the class that implements this method
It must be
Subclass of the member's class.
7. native
Intended audience: Members
Description: 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
Objects used: classes and Methods
Introduction: all methods in the strictfp modifier class hide the strictfp modifier and all floating points of method execution.
Computing compliance
IEEE 754 standard. All values, including intermediate results, must be of the float or double type, but cannot be used.
Floating by local platforms
The extra precision or representation range provided by the point format or hardware.
9. synchronized
Usage object: Method
Introduction: For a static method, jvm locks the class in which it belongs before execution. For a non-static class
Method, execution
Before locking a specific object instance.
10. volatile
Use object: Field
Introduction: Because the asynchronous thread can access fields, some optimization operations cannot be applied to fields.
Volatile sometimes
Synchronized can be replaced.
11. transient
Use object: Field
Description: fields are not part of the object's persistent State and should not be concatenated with objects.