java modifier symbol type:
The most restrictive modifier in the Public:java language, commonly referred to as "public". The classes, properties, and methods that are decorated are not only accessible across classes, but also across packages (package).
The narrowest modifier for access restrictions in the Private:java language is generally referred to as "private." Classes, properties, and methods that are modified by them can only be accessed by objects of that class, their subclasses cannot be accessed, and cross-package access is not allowed.
Protected: An access modifier between public and private, commonly referred to as a "protected form." Classes, properties, and methods that are modified by them can only be accessed by methods and subclasses of the class itself, even if the subclasses are accessible in different packages.
Default: That is, without any access modifiers, often referred to as the "Default access mode." In this mode, access is allowed only in the same package.
(i) class
Access modifier modifier Class class name extends parent class name Implement Interface name
(The location of the access modifier and the modifier can be interchanged)
Access modifiers |
Name |
Description |
Note |
Public |
can be accessed by all classes (using) |
The public class must be defined in a file of the same name with the same class name |
Package |
can be accessed by a class in the same package (using) |
The default access rights, which can be omitted from this keyword, can be defined in the same file as the public class |
Modifier |
Name |
Description |
Note |
Final |
A class that uses this modifier cannot be inherited |
|
Abstract |
If you want to use the abstract class, you must first build a new class that inherits the abstract class, and the new class implements an abstraction method of the abstract class. |
class must be defined as an abstract class as long as there is an abstract method, but the abstract class does not necessarily need to protect the abstract method. |
(ii) Variables
1. There are no global variables in Java, only method variables, instance variables (non-static variables in the Class), class variables (static variables in the Class).
2. Variables in the method cannot have access modifiers. So the following access modifiers are only for variables defined in the class.
3. When declaring an instance variable, if no initial value is assigned, it will be initialized to null (reference type) or 0, False (original type).
4. You can initialize a more complex instance variable with an instance variable initializer, which is a block of statements with {}, which runs after the constructor of the class is invoked, before the constructor of the parent class constructor.
5, class variables (static variables) can also be initialized by the class variable initializer, the class variable initializer is a block of statements contained in static{}, and may only be initialized once.
Access modifiers |
Name |
Description |
Note |
Public |
can be accessed by any class |
|
Protected |
can be accessed by all classes in the same package, accessible by all subclasses, and can be cross-wrapped by subclasses. |
Subclasses are not accessible in the same package |
Private |
can only be accessed by methods of the current class |
Subclass cannot access |
Default |
can be accessed by all classes in the same package |
If the child class is not in the same package, it is inaccessible. |
Modifier |
Name |
Description |
Note |
Static |
Static variables (also known as class variables, others are called instance variables) |
Can be shared by all instances of the class |
Final |
Values can only be assigned once and cannot be changed |
Be careful not to use const, although it has the same meaning as the Const keyword in C, C + +, and can be used with static to avoid maintaining a copy of each instance of the class. |
Transient |
Tells the compiler that this variable does not need to persist when the class object is serialized |
Mainly because the amount of change can be obtained by other variables, it is used for performance problems |
Volatile |
Indicates that there may be multiple threads modifying this variable, requiring compiler optimizations to ensure that modifications to this variable are handled correctly |
|
(iii) methods
Access modifier modifier return type method name (parameter list) throws violation list
1. The constructor method of a class can not have modifiers, return types, and throws clauses.
2. When the constructor method of a class is called, it first calls the constructor method of the parent class, and then runs the initializer for the instance variable and the static variable before the constructor itself is run. (Very important)
3. If the constructor method does not display a constructor that calls a parent class, the compiler automatically adds a default super () to it, and if the parent does not have a default parameterless constructor, the compiler will give an error. Super must be the first clause of a constructor method. (Very important)
4, pay attention to understand the use of private constructor method techniques.
Access modifiers |
Name |
Description |
Note |
Public |
can be accessed from all classes |
|
Protected |
can be accessed by all classes in the same package, and can be accessed by all subclasses. |
Subclasses are not in the same package and can be accessed |
Private |
can only be accessed by methods of the current class |
|
Default |
can be accessed by all classes in the same package |
If the subclass is not in the same package, you cannot access the |
Modifier |
Name |
Description |
Note |
Static |
Static methods (also known as class methods, others called instance methods) |
Provides services that do not depend on class instances, and you do not need to create an instance of the class to access static methods. |
Final |
Prevent any subclasses from overloading the method |
Be careful not to use const, although it has the same meaning as the Const keyword in C, C + +, and can be used with static to avoid maintaining a copy of each instance of the class. |
Abstract |
Abstract method, a method that has been declared in a class and not implemented. |
You cannot declare a static method, a final method, or a constructor method of a class as abstract |
Native |
The method defined with this modifier is not implemented in the class, and in most cases the implementation of the method is written in C and C + +. |
See Sun's Java Native Interface (JNI), which provides the runtime to load an implementation of a native method and associate it with a Java class function |
Synchronized |
Multi-threaded support |
When one of these methods is called, no other thread can call the method, and the other synchronized method cannot call the method until the method returns |
(iv) interface
Access modifier Interface Interface Name extends interface list
1. The interface is not able to define any implementation of the method it declares.
2, the variables in the interface must always be defined as "public static final interface name", but can not contain these modifiers, compiler default is the case, the display contains modifiers mainly for the program is clear.
Access modifiers |
Name |
Description |
Note |
Public |
All |
|
No access modifier |
In the same package |
|
PS: This article turns from "On the road" of the "Java modifier type (public,protected,private,friendly)", only for learning, communication.
Java modifier types (public, protected, private) summary