java keyword
final
data
Basic data types: make the value unchangedReference data types: the reference is unchanged, that is, it cannot refer to other objects. But the referenced object can be modified
method
Declared methods cannot be overridden by subclasses.Private methods are implicitly designated as final. If a method defined in a subclass has the same signature as a private method in the base class, the subclass method does not override the base class method, but defines a method in the subclass. new method.
kind
Classes cannot be inheritedstatic
1. Static variables
Static variables exist only once in memory and are only assigned once when the class is initialized.
- Static variables: All instances of a class share static variables, which can be accessed directly through the class name;
- Instance variables: Every time an instance is created, an instance variable is created, which lives and dies with the instance.
public class A {
private int x; // instance variable public static int y; // static variable}
2. Static methods
The static method exists when the class is loaded, it does not depend on any instance, so the static method must be implemented, that is to say it cannot be an abstract method (abstract).
3. Static block
Static statement blocks are run once when the class is initialized.
4. Static inner class
A type of inner class, a static inner class does not depend on the outer class, and cannot access the non-static variables and methods of the outer class.
5. Static import package
import static com.xxx.ClassName.*
No need to specify the ClassName when using static variables and methods, which simplifies the code, but greatly reduces the readability.
6. Variable assignment order
The assignment of static variables and the execution of static statement blocks take precedence over the assignment of instance variables and the execution of ordinary statement blocks. The assignment of static variables and the execution of static statement blocks are executed first depending on their order in the code.
public static String staticField = "static variable ";
static {
System .out .println( "static statement block "); }
public String field = "instance variable ";
{
System .out .println( "normal block "); }
Run the constructor at the end
public InitialOrderTest() {
System .out .println( "Constructor "); }
In the presence of inheritance, the initialization sequence is:
- Parent class (static variable, static statement block)
- Subclasses (static variables, static statement blocks)
- Parent class (instance variables, normal statement blocks)
- parent class (constructor)
- Subclasses (instance variables, normal statement blocks)
- subclass (constructor)