Features of the Static keyword
- Loaded with the load of the class;
- Precedence over the existence of objects;
- Be shared by all objects of the class;
- Can be called by the class name;
Memory Diagram Construction code block
Constructing a code block takes precedence over the construction method execution, and creating an object executes once;
publicclass User { privateint age; publicUser(int age) { System.out.println("构造方法执行"); this.age = age; } publicintgetAge() { return age; } publicvoidsetAge(int age) { this.age = age } { System.out.println("构造代码块执行"); } }
Output Result:
Constructing code block execution
Construction method Execution
Final keyword
- In inheritance, when a method of the parent class is modified with final, the subclass inherits the parent class, and the subclass cannot modify the final decorated method in the parent class;
The Static keyword in Java