The use of static blocks of code in Java

  • 2020-04-01 01:43:42
  • OfStack

Comparison with static methods

In general, if some code must be executed at the start of the project, and static blocks are needed, this code is actively executed. You need to initialize it at the start of the project, and instead of creating an object, when other programs call it, you need to use static methods, which are already loaded when the class is loaded and you can call it directly with the class name so the main method has to be static and this is the entry to the program. The difference is that static blocks of code are executed automatically; Static methods are executed when they are called.


Two. Static method notes

When using static methods of a class, note:

A. In a static method, you can only directly call other static members of the same class (including variables and methods), but you cannot directly access non-static members of the class. This is because, for non-static methods and variables, you need to create an instance object of the class before you can use it, and for static methods, before you use them

You don't create any objects.

B. Static methods cannot refer to this and super keywords in any way, because static methods do not create any instance objects before they are used, and when static methods are called, the object referenced by this is not generated at all

A reference to "objects").

Static variables are variables that belong to an entire class, not to an object. Note that variables in any method body cannot be declared static, for example: fun() {      Static int I = 0; / / illegal. }
 

Examples of procedures


public class TestStaticCon {
     public static int a = 0;

     static {
         a = 10;
         System.out.println(" The parent class's static block of code is executing a=" + a);
     }

     {
         a = 8;
         System.out.println(" The parent class's non-static block of code is executing a=" + a);
     }

     public TestStaticCon() {
         this("a The value in the parent class parameter constructor: " + TestStaticCon.a); //Call another constructor
         System.out.println(a);
         System.out.println(" The parent class parameter - free constructor is executing 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();
     }
 }


 Operation results:  
 The parent class's static block of code is executing a=10 
!!!!!!!!!!!!!!!!!!!!! 
 The parent class's non-static block of code is executing a=8 
a The value in the parent class parameter constructor: 10 
8 
8 
 The parent class parameter - free constructor is executing a=8 

Four. The net friend provides

public class StaticBlock {

     static {
         System.out.println(" A static block ");
     }
     {
         System.out.println(" A building block, defined in a class ");
     }

     public StaticBlock() {
         System.out.println(" Constructor execution ");
     }

     public static void main(String[] args) {
         new StaticBlock();
         new StaticBlock();
     }

 }


 A static block  
 A building block, defined in a class  
 Constructor execution  
 A building block, defined in a class  
 Constructor execution  

Conclusion: a static block of code is automatically executed when a class is loaded. A non-static block of code is the code that is automatically executed when an object is created. And the execution order is static code block -- non - static code block - constructor.

One of the things that got me confused was "the value of a in the parent class parameter constructor: 10 ", I want to then why not 8 again, a debug (F11, not directly to set breakpoints and then run, run directly like that no difference), found that the first kick-started no-parameter constructor, performed the first statement and switch to another constructor (or not since the first sentence to be executed, at this point a or 10, the static block of code has not been performed), prompt can not find the source, regardless of whether this statement suggests the warning (not wrong, because the program continues to run normally), then run the non-static blocks of code, which in turn from a no-parameter constructor to continue...


Related articles: