Function:
(Recommended tutorial: java introductory tutorial)
(1) Static variables: also known as classes Variable, that is to say, this variable belongs to the class. All instances of the class share static variables and can be accessed directly through the class name. There is only one copy of static variables in memory;
(2) Static method: Static method exists when the class is loaded, and it does not depend on any instance. So the static method must have an implementation, which means it cannot be an abstract method. Only static fields and static methods of the class can be accessed, and the this and super keywords cannot be included in the method;
(3) Static statement block: The static statement block is run once during class initialization;
(4) Static inner classes: Non-static inner classes depend on instances of outer classes, while static inner classes do not. Static inner classes cannot access non-static variables and methods of outer classes;
(5) Initialization sequence: static variables and static statement blocks take precedence over instance variables and ordinary statement blocks, and static variables and static statement blocks are initialized The order depends on their order in the code.
(Video tutorial recommendation: java video tutorial)
Code example:
Output result:
The above is the detailed content of What is the function of static keyword in java. For more information, please follow other related articles on the PHP Chinese website!