Class Declaration
[
Public] [
Abstract] [
Final]
Class
NameOfClass [
Extends
Super] [
Implements
Interfaces]
Pubilc: declares that this class can be used in any other class. This class can only be used by other classes in the same package.
Abstract: declares that this class is an abstract class and cannot be instantiated. An abstract class can contain abstract methods, but the abstract method is not implemented. Therefore, the abstract class does not have actual functions and is only used to derive subclasses.
Final: declares that the class cannot be inherited, that is, there is no subclass. You cannot create a new class by extending this class.
Extends: Declares the parent class of the class. A subclass inherits variables and methods from its parent class. In
In Java,
In addition to the Object class, each class has a parent class. If a class has no parent class,
Java considers it to be
Object Class subclass, so all other classes are
The direct or indirect subclass of the Object class. Note that
The extends keyword can only be followed by a unique parent class name.
Extends can only implement Single inheritance.
Implements: declares one or more interfaces to be implemented by this class.
After the implements keyword, the names of the interfaces implemented by this class are given and separated by commas.
Variable Declaration
[Accessability modifier] [
Static] [
Final] [
Transient] [
Volatile] Type
Variable name
Accessability modifier: later.
Static: declare this variable as a class variable to differentiate General instance variables. All instances of class variables use the same copy, which belongs to the public domain of the class and is shared by all objects of the class. It can be considered as a global variable within the class range. The static variable value is changed by the static method.
Final: declare this variable as a constant. Use keywords
When final declares a constant, it must specify the Data Type of the constant and the specific value of the constant.
Transient: declares this variable as a transient variable, which is not a persistent part of the object.
Volatile: declares that the variable may be controlled or modified simultaneously by multiple threads in the concurrent operation. That is, the variable is not controlled by the current program, in addition, other unknown program operations may affect and change the value of this variable during the running process.
The volatile keyword sends this information
Java operating system.
The Volatile keyword is usually used to modify the domain that accepts external input.
Method Declaration
[Accessability modifier] [
Static] [
Abstract] [
Final] [
Native] [
Synchronized] return type
Method Name (parameter table )[
Throws
Exception table]
Static: declare this method as a class method. Class methods have the following three considerations:
When calling a class method, you should use the class name as the prefix, instead of using a specific object name;
Non
The static method belongs to the object method. Therefore, when creating an object
The static method has its own code segment in the memory allocated to the object.
The static method belongs to the entire class. The code segment in the memory is loaded with the definition of the class and is not owned by any object;
The static method cannot manipulate or process member variables of an object, but can only process class variables of the entire class, that is
The static method can only process
Static variable.
Abstract: declare a method as an abstract method, that is, there is only a method declaration part, but there is no specific method body and operation implementation method. Note that abstract methods must exist in abstract classes. An abstract method is invalid in a non-abstract class, that is, if a subclass of an abstract class is not an abstract class, the method body must be written for all abstract methods in the parent class. Of course, abstract classes may not only have abstract methods, but also include non-abstract methods.
Final: declare this method as the final method. The final method cannot be redefined by the subclass of the current class. Note that all
The private modifier is limited to private methods and all
Methods In the final class are considered as the final method by default. Because these methods are either not inherited by the quilt class or have no subclass at all, they cannot be overloaded. Naturally, they are the final method.
Native: special method for declaring method bodies written in other languages.
C,
C ++,
FORTRAN and assembly language.
Native methods are written externally in other languages, so all
No method body is available for native methods.
Synchronized: declare this method as a synchronization method.
Synchronized modifiers are mainly used for coordination and synchronization in multi-threaded coexistence programs. If
The synchronized keyword modifies the class method. Before this method is called and executed, the system class
Lock the object corresponding to the current Class in Class; if
The synchronize keyword modifies the instance method. The current object is locked before the method is called.
Access Control for Class Members
By default, a class as a whole can be accessed and referenced by classes in the same package; if the class is declared
Public, then use
After the import Statement introduces this class, it can be accessed and referenced by all other classes.
However, a class is visible to other parts of the program as a whole, which does not mean that all the fields and methods of the class are also visible to other parts of the program, you also need to check the access controllers of these domains and methods.
Java supports four access levels for variables and methods:
Private,
Protected,
Public and default modifier. The following table lists the access permissions for each access level.
Table
1
:
Access permission of the access modifier
Accessability Modifier |
Class |
Subclass |
Package |
All |
Private |
Yes |
|
|
|
Protected |
Yes |
Yes |
Yes |
|
Public |
Yes |
Yes |
Yes |
Yes |
Default |
Yes |
|
Yes |
|
Reference :《
Compiled by Zhu Fuxi and Java language and object-oriented programming.