Access
Add the basics of Java (turn)
A description of various modifiers and access modifiers in Java
Class:
Access modifier class class name extends parent class name Implement Interface name
(The location of the access modifier and modifier can be interchanged)
Access modifiers
Name
Description
Note
Public
can be accessed by all classes (used)
The public class must be defined in a file with the same name as the class name
Package
can be accessed by classes in the same package (used)
Default access permissions, you can omit this keyword and define it in the same file as the public class
Modifiers
Name
Description
Note
Final
A class that uses this modifier cannot be inherited
Abstract
If you want to use an abstract class, you must first build a new class that inherits the abstract class, and the new class implements the abstract methods in the abstract class.
class must be defined as abstract if it has an abstract method, but the abstract class does not have to protect the abstract method
Variable
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).
The variable in the L method cannot have an access modifier. So the following access modifier chart is only for variables defined in the class.
L When declaring an instance variable, if no initial value is assigned, it is initialized to null (reference type) or 0, False (original type).
L A more complex instance variable can be initialized by an instance variable initializer, which is a block of statements with {}, which runs when the constructor of the class is invoked, and before the constructor, after the parent class constructor.
The L class variable (static variable) can also be initialized by the class variable initializer, which 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
can be accessed by all child classes
Subclasses are not available in the same package
Private
can only be accessed by methods of the current class
Default
No access modifiers
can be accessed by all classes in the same package
If the subclass is not in the same package, it cannot access the
Modifiers
Name
Description
Note
Static
Static variables (also called class variables, others are called instance variables)
Can be shared by all instances of the class.
You do not need to create an instance of a class to access static variables
Final
constants, values can be assigned only once, and cannot be changed
Be careful not to use const, although it has the same meaning as the Const keyword in C and C + +
Can be used with static to avoid maintaining a copy of each instance of a class
Transient
Tells the compiler that this variable does not need to be persisted when the class object is serialized
The main reason is that the amount of change can come through other variables, using it for performance problems
Volatile
Indicates that multiple threads may modify this variable, requiring compiler optimizations to ensure that modifications to this variable are handled correctly
Method
Access modifier modifiers return type method name (argument list) throws list of violations
The constructor method of the L class can not have modifiers, return types, and throws clauses
When the constructor method of the L class is invoked, it first invokes the constructor method of the parent class, and then runs the initializer for the instance and static variables before running the constructor itself.
L If the constructor method does not display a constructor that invokes a parent class, then the compiler automatically adds a default super () to it, and if the parent class does not have a default parameterless constructor, the compiler will complain. Super must be the first clause of the constructor method.
L pay attention to the skill of using private constructor methods.
Access modifiers
Name
Description
Note
Public
can be accessed from all classes
Protected
can be accessed by all classes in the same package
can be accessed by all child classes
Subclasses are not available in the same package
Private
can only be accessed by methods of the current class
Default
No access modifiers
can be accessed by all classes in the same package
If the subclass is not in the same package, it cannot access the
Modifiers
Name
Description
Note
Static
Static methods (also called class methods, others are called instance methods)
Provide services that are not dependent on class instances
You do not need to create an instance of the class to access the static method
Final
Prevent overloading of this method by any child classes
Be careful not to use const, although it has the same meaning as the Const keyword in C and C + +
Can be used with static to avoid maintaining a copy of each instance of a class
Abstract
Abstract method, a method declared in a class that has not been implemented
You cannot declare the static method, final method, or constructor method of a class as abstract
Native
Methods defined with this modifier are 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), JNI provides runtime-loaded implementation of a native method and functions associated with a Java class
Synchronized
Multi-threaded support
When one of this method is invoked, no other thread can call the method, nor can other synchronized methods call the method until the method returns
Interface
Access modifier Interface Interface Name extends interface list
The L interface cannot define any implementation of its declared method
The variables in the L interface always need to be defined as "public static final interface names", but they are not included in the compiler, which is the default, and the display contains modifiers are mainly for program clarity
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.