static: (static modifier) The content of static modification in an object-oriented object belongs to the class, not directly affiliated to the object, so the member variables modified by static are generally called class member variables, and the method of static modification is generally called class methods.
Classification:
1. Static variables, also called static variables or class variables. Another type is a variable that is not modified by static, called an instance variable.
2. Static method, also called static method or class method, cannot define static variables in static methods, and instance methods cannot.
3. Static code block, in a static block, you can access static variables and call static methods.
Notes:
1. If static does not depend on any object, there is no this method.
2. The static method cannot call a non-static method, but a non-static method can call a static method.
3. Methods or variables modified by static do not need to rely on objects for access. As long as the class is loaded, they can be accessed through the class name.
4. The static method cannot be rewritten. When the subclass and the parent class have the same static method, the default call is the parent class's static method, and the subclass's static method is hidden.
5. Static blocks are generally used to initialize static variables in classes. Based on the content of static modifications, the principle of first definition and first execution is followed.
6. If variables or methods are often called, use static modification. Otherwise, use it less to avoid memory leakage.
Example code of static variables:
public class Test{static int a=1; //static variable int b=2; //Instance variable public static void main(String[] args){System.out.println(Test.a); //System.out.print(a);System.out.println(b); //Directly output variable b will report an error Test t=new Test(); //Create instance object System.out.println(tb); //Object calls variable}}Example of code for static methods:
public class Test{static int a=1; //static variable int b=2; //Instance variable static void A(){ //static method B(); //An error is reported, the static method cannot call the non-static method System.out.println(Test.a); //System.out.print(a);}void B(){ //Non-static method Test.A(); //A(); Non-static method can call the static method System.out.println(b); System.out.println(Test.a); //System.out.print(a);}public static void main(String[] args){Test.A(); //A();B(); //An error is reported, non-static methods need to be called through an instance object Test t=new Test();tB();}}/* * Can it be rewritten*/class StaticSon extends Test{ //Subclass inherits the parent class void A(){ //An error is reported, the static method of the parent class cannot be rewritten/*...*/}static void A(){ //This is a static method of the subclass, strictly speaking, it is not a static method of the parent class}}Example of static block code:
public class Test{int c=3;static int a=1;static int b=2;static{a=10;b=20;c=30; //Report an error, the variable in the static block must be the static variable System.out.println(a+b);}public static void main(String[] args){Test t=new Test();System.out.println(tc); //Execute the static variable first, then execute the instance variable}} final: Understand as immutable.
Notes:
1. Final and static are often used together.
2. The variables modified by final can only be assigned once.
3. The final method cannot be rewritten, but can be overloaded.
4. The final class cannot be inherited.
5. The two keywords final and abstract are opposite, and it is impossible to modify the class at the same time. Because final cannot be rewritten, and abstract must be rewritten.
6. The final keyword is different from the final keyword, which is used for exception handling.
7. In most cases, final is not used to modify methods and classes because they are not scalable.
8. Final can be used in a certain environment to improve the running performance of the program and optimize the structure of the program.
Example of code of final variables and final classes:
public final class Test{ final static int a=1; static { a=10; // Report an error, only one assignment operation can be performed}}class FinalSon extends Test{ // Report an error, the final class cannot be inherited}Example code of final method:
public class Test{final static int a=1;final void A(){ //final method System.out.println(a);}}class FinalTest extends Test{void A(){ //An error is reported, the final method cannot be rewritten System.out.println("err");}}final abstract class FinalErr{ //final and abstract cannot exist at the same time/*......*/}