Final――
Final is used to indicate that the class cannot derive subclasses.
Final is used to indicate that the method cannot be rewritten by a subclass.
Final is used to represent constants when variables, similar to the const keyword in C/C++.
Final is used for member variables to indicate that the member variable is a constant and cannot be modified. It must be assigned a value when the variable is defined.
Final is used to indicate that the local variable is a constant and cannot be modified. You can assign a value when the variable is defined, or you can define the variable first and then assign a value.
static---
static is used to indicate that there is only one copy of the variable, that is, the static member variable belongs to the class but not to a specific class instance object. All class instance objects share this static member variable, and when accessing the static member variable, the corresponding Class name is completed. Static member variables can be initialized when defined or not. They will be automatically initialized when not assigned. Remember that local variables cannot be static.
static is used for methods, so that static methods can be called through class names without instantiating the class. It should be noted that this keyword cannot be used within static methods, non-static methods cannot be called, and non-static members cannot be referenced. variable.
static is used for classes, which refers to inner class here, so you can refer to this static inner class through the outer class name elsewhere.
static can also be used for class code blocks, called static code blocks. They are static statement blocks independent of class members in the class. They can have multiple locations and can be placed at will. They are not in any method body. When the JVM loads the class, it will Execute these static code blocks. If there are multiple static code blocks, the JVM will execute them in sequence in the order they appear in the class, and each code block will be executed only once.
Class access permissions (Y/N)
|| Keywords || Class || Package || Subclass || Other Packages ||
| public | Y | Y | Y | Y | Y |
| protected | Y | Y | Y | N |
| default | Y | Y | N | N |
| private | Y | N | N | N |
The three keywords public, protected and private can be used for classes (internal classes), member variables and member functions. The default access permission is valid in the package. When the class access permission is different from the access permission of member variables or member functions, Select the lowest access permission.
interface/implements/extends/class――
The interface is used to declare the interface. The methods in the interface are only declared and not implemented. The access permission can be public or default permissions, and can also be specified as abstract.
Implements are used to implement interfaces, and all methods in the interface are required to implement multiple interfaces at the same time.
extends is used to inherit the parent class or parent interface. When inheriting the parent class, it can only be single inheritance. Unlike C++'s multi-inheritance, interface inheritance supports multiple inheritance.
class is used to declare classes. Access permissions can be public or default permissions, and can also be specified as abstract or final. Access permissions have different restrictions on top-level classes and internal classes.
abstract―
abstract is used to represent that this class is an abstract class and cannot be instantiated.
abstract is used to represent that this method is an abstract method. It only needs to be declared, not implemented, and is implemented by subclasses. Abstract methods cannot be made with private and static keywords.
Let’s focus on the use of this and super:
This
The Java keyword this can only be used in the method body. When an object is created, the Java virtual machine (JVM) will assign a pointer to the object that refers to itself, and the name of this pointer is this. Therefore, this can only be used in non-static methods in the class. This must not appear in static methods and static code blocks. This is a clear explanation in the article "Summary of the use of Java keyword static and final". And this is only associated with specific objects, not with classes, and different objects of the same class have different this. Here is a comprehensive example using this to illustrate the problem:
package org.leizhimin;public class Test6 { private int number; private String username; private String password; private int x = 100; public Test6(in t n) { number = n; // This can also be written as: this.number= n; } public Test6(int i, String username, String password) { // Member variables and parameters have the same name, and the member variable is blocked, and the member variable is accessed in the way of "this. member variable". this.username = username; this. password = password; } // The default constructor without parameters public Test6() { this(0, "Unknown", "Empty"); // Call another constructor via this} public Test6(String name) { this (1, name, "empty"); // Call another constructor through this} public static void main(String args[]) { Test6 t1 = new Test6(); Test6 t2 = new Test6("Guest"); t1.outinfo(t1); t2.outinfo(t2); } private void outinfo(Test6 t) { System.out.println("------------------"); System.out.println( t.number); System.out.println(t.username); System.out.println(t.password); f(); // This can be written as: this.f(); } private void f() { // Local variables have the same name as member variables, and the member variable is blocked, and the member variable is accessed in the form of "this. member variables". int x; x = this.x++; System.out.println(x); System.out. println(this.x); } //Return the reference to the current instance private Test6 getSelf() { return this; }}
The operation results are as follows:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Looking at the example above, we will explain under what circumstances this is needed:
First, call another constructor through this, and the use is this (parameter list). This is only used in the class constructor and cannot be used in this way elsewhere.
Second, when the function parameters or local variables in the function have the same name as the member variable, the member variable is blocked. At this time, to access the member variable, you need to use the "this. member variable name" to reference the member variable. Of course, without the same name, you can use the name of the member variable directly instead of this, it is not wrong to use it, haha.
Third, in the function, when you need to refer to the current object of the class to which the function belongs, use this directly.
In fact, these usage summary are all derived from a deeper understanding of the sentence "this is a pointer to the object itself". It is easy to forget or make mistakes if it is memorized by rote. You must understand it!
super
The key to super is similar to this, which is that the blocked member variable or member method becomes visible, or is used to refer to the blocked member variable and member member method.
However, super is used in subclasses, with the purpose of accessing blocked members in direct parent class. Note that it is the direct parent class (that is, the most recent superclass above the class). The following is an example of comprehensively using super, with two classes: a Father class and a subclass of Father class Son. The usage of super is fully demonstrated through these two classes. Here is the code:
package org.leizhimin;public class Father { public String v="Father"; public String x="Export the public member variable x!!!"; public Father() { System.ou t.println("Father construct The method is called!"); } public Father(String v){ this.v="Father class's parameter constructor method! Running."; } public void outinfo(){ System.out.println("Father's outinfo The method is called "); } public static void main(String[] args) { // TODO automatically generates method stubs}} package org.leizhimin;public class Son extends Father{ public String v="Son"; public Son() { super(); //Calling the superclass constructor method, it can only be placed in the first line. System.out.println("Son parameterless constructor method is called!"); //super(); //Incorrect , it must be placed in the front of the constructor body. } public Son(String str){ super(str); System.out.println("Son constructor method with parameter is called!"); } // Overwrites superclass members Method outinfo() public void outinfo(){ System.out.println("Son's outinfo() method is called"); } public void test(){ String v="hahahaha!"; // Local variable v overwrite Member variable v and superclass variable v System.out.println("-----------"); System.out.println(v); //Output local variable v System.out. println(this.v); //Output (subclass) member variable v System.out.println(super.v); //Output superclass member variable v System.out.println("------2 -----"); System.out.println(x); //Output superclass member variable v, subclass inherits from System.out.println(super.x); //Output superclass member variable v System.out.println("-----------"); outinfo(); //Calling the outinfo() method of the subclass this.outinfo(); //Calling the outinfo( ) method super.outinfo(); //Calling the outinfo() method of the parent class} public static void main(String[] args) { new Son().test(); }}
Subclass Son run results:
The Father constructor is called! The Son parameterless constructor is called!-------1------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- x!!! The public member variable of the Father class output x!!!-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------