Description of Various modifications and access modifiers in Java

zhaozj2021-02-17  50

Description of Various modifications and access modifiers in Java

class:

Access modifier modifier Class class name EXTENDS Parent Class Name Implement Interface Name

(Access the position of the modifier and the modifier can be exchanged)

Access modifier

name

Description

Note

Public can be accessed (used) PUBLIC classes must be defined in the same name file in the same name in the same name. Package can be accessed (used) by default access (use), which can be omitted, which can be defined and In the same file of the PUBLIC class

Modifier

name

Description

Note

Final uses this modifier class that cannot be inherited

Abstract If you want to use the Abstract class, you must first build a new class that inherited the Abstract class, and implements abstract methods in the Abstract class in the new class. Class As long as there is an Abstract method, the class must be defined as Abstract, but the Abstract class is not necessarily to protect the Abstract method.

variable

l Java has no global variables, only method variables, instance variables (non-static variables in classes), class variables (static variables in classes).

The variable in the L method cannot have access to the modifier. So the following access to the modifier is only for variables defined in the class.

l When declaring instance variables, if it is not assigned, it will be initialized to NULL (reference type) or 0, false.

l You can initialize more complex instance variables by an instance variable initializer. The instance variable initializer is a statement block included with {}. When the structure of the class is called, after the parent class constructor, the constructor before .

The L-class variable (static variable) can also be initialized by a class variable initializer, and the class variable initializer is a statement block included with static {}, which may only be initialized once.

Access modifier

name

Description

Note

PUBLIC can be accessed by any class

Protected can be accessed by all kinds of access in the same package that can be accessed in the same package in the same package in the same package, can also be accessed by the current class.

The default no access modifier can be accessed by all kinds of classes in the same package If the subclass is not in the same package, it is not accessible

Modifier

name

Description

Note

Static static variable (also known as class variable, other variables, called instance variables) can be shared by all instances of the class. If you do not need to create an instance of the class, you can access the static variable Final constant. You can only assign it once. You cannot change it. Don't use const, although it can be used with Static, avoiding the class with Static. Each instance maintains a copy of Transient tells the compiler. When the class object is serialized, this variable does not require persistent storage. It is mainly because the amount of change can be obtained by other variables. It is used for performance Volatile points to the possibility. A thread modifies this variable, requiring the compiler to optimize to ensure that the changes to this variable can be properly processed

method

Access to modifier modifiers return Type method name (parameter list) throws violation list

The L-class constructor method is not able to have a modifier, return type, and throws clause.

When the L-class constructor is called, it first calls the constructor method of the parent class, then runs the instance variable and the initializer of the static variable, and then run the constructor itself.

l If the constructor is not displayed to call a parent class constructor, the compiler will automatically add a default super (), and if the parent class does not have the default non-parameter constructor, the compiler will report an error. . Super must be the first clause of the constructor method. l Take User Tips for Understanding the Private Constructor Method.

Access modifier

name

Description

Note

PUBLIC can access all kinds of access

Protected can be accessed by all kinds of access in the same package that can be accessed in the same package in the same package in the same package, can also be accessed by the current class.

The default no access modifier can be accessed by all kinds of classes in the same package If the subclass is not in the same package, it is not accessible

Modifier

name

Description

Note

Static static method (also known as class method, other method method) provides an instance that does not depend on class instance does not need to create an instance of the class, you can access the static method Final to prevent any subclasses from overloading this method not to use Const Although it can be used with Static, it can be used with Static, avoiding each instance of the class to maintain a copy Abstract abstract method, and the method in the class has declared without implementing the static method, FINAL method. Or the class constructor method declares that the method defined by this modifier is not implemented in the class, while most cases, the implementation of the method is written in C, C . See Sun's Java Native Interface (JNI), JNI provides an implementation of a Native method to load, and the function of a Java class associated with the function synchronized multi-threaded support When one this method is called, no other thread can call This method, the other Synchronized method cannot call the method until the method returns

interface

Access the modifier interface interface name EXTENDS interface list

l Any implementation of the interface that cannot define its declaration

l The variables in the interface always need to be defined as "public static final interface name", but may not contain these modifiers, the compiler is the default, the displayed modifier is mainly for program clarity.

Access modifier

name

Description

Note

PUBLIC

No access modifier (default) in the same package

转载请注明原文地址:https://www.9cbs.com/read-28648.html

New Post(0)