1. Comparison with static methods
Generally speaking, if some code must be executed when the project is started, you need to use static code blocks. This kind of code is actively executed; it needs to be executed automatically when the project starts. The project is initialized when it starts. Without creating an object, static methods need to be used when called by other programs. Static methods are already loaded when the class is loaded and can be called directly using the class name. For example, the main method must be static. This is the program entry. The difference between the two is that static code blocks are executed automatically; static methods are executed when called.
2. Static Method Precautions
When using static methods of a class, please note:
a. In a static method, you can only directly call other members of the same class. Static members (including variables and methods), while non-static members in a class cannot be directly accessed. This is because non-static methods and variables need to create an instance object of the class before they can be used, while static methods do not need to create any objects before use.
b. Static methods cannot reference this and super keywords in any way, because static methods do not need to create any instance objects before use. When the static method is called, the object referenced by this is not generated at all (this key The word can only be used inside a method and represents a reference to "the
object that called the method").
Static variables are variables that belong to the entire class rather than to an object. Note that variables within any method body cannot be declared static, for example: fun() { static int i=0;//Illegal. }
3. Program examples
public class TestStaticCon { public static int a = 0; static { a = 10; System.out.println("父类的静态代码块在执行a=" + a); } { a = 8; System.out.println("父类的非静态代码块在执行a=" + a); } public TestStaticCon() { this("a在父类带参构造方法中的值:" + TestStaticCon.a); // 调用另外一个构造方法 System.out.println(a); System.out.println("父类无参构造方法在执行a=" + a); } public TestStaticCon(String n) { System.out.println(n); System.out.println(a); } public static void main(String[] args) { TestStaticCon tsc = null; System.out.println("!!!!!!!!!!!!!!!!!!!!!"); tsc = new TestStaticCon(); } }
运行结果: 父类的静态代码块在执行a=10 !!!!!!!!!!!!!!!!!!!!! 父类的非静态代码块在执行a=8 a在父类带参构造方法中的值:10 8 8 父类无参构造方法在执行a=8
4. Provided by netizens
public class StaticBlock { static { System.out.println("静态块"); } { System.out.println("构造块,在类中定义"); } public StaticBlock() { System.out.println("构造方法执行"); } public static void main(String[] args) { new StaticBlock(); new StaticBlock(); } }
静态块 构造块,在类中定义 构造方法执行 构造块,在类中定义 构造方法执行
Conclusion: Static code blocks are automatically executed when classes are loaded, non-static code blocks It is code that is automatically executed when an object is created. Non-static code blocks of this class will not be executed if the object is not created. And the execution order is static code block------non-static code block--constructor.
What puzzles me is "the value of a in the parent class's constructor method with parameters: 10". I then thought about why it was not 8 at that time. Debug (F11, you cannot directly set a breakpoint and then run it. That is no different from running it directly), and found that it entered the parameterless constructor first, executed the first statement and switched to another constructor (the first sentence must be executed regardless of whether it is, at this time a is still 10, otherwise The static code block has not yet been executed), prompting that the source cannot be found, regardless of whether this statement prompts this warning (not an error, because the program continues to run normally), and then runs the non-static code block, and then from the parameter Execution continues at the constructor method...
For more detailed explanations on the use of static static code blocks in Java, please pay attention to the PHP Chinese website for related articles!