Common code blocks construction code blocks static code block differences and code samples in Java

  • 2020-05-27 05:41:30
  • OfStack

Common code blocks, construction code blocks, static code block differences and code samples in Java

// order of execution :(priority from high to low) Static code block > mian method > Construct code block > Constructors.

Where the static block of code is executed only once. Construction blocks are executed every time an object is created.

1 common code block


// Common code block: occurs in a method or statement {} This is called a normal block of code. Common code blocks and 1 The order in which statements are executed is determined by the order in which they appear in the code -- "First come, first served" 
public class CodeBlock01{
   public static void main(String[] args){
     
      {
       int x=3;
       System.out.println("1, A variable inside a normal code block x="+x);  
      }
      
      int x=1;
      System.out.println(" Variables within the main method x="+x);
      
      {
        int y=7;
        System.out.println("2, A variable inside a normal code block y="+y);  
      }
     }
  }
  
  /*
   Operation results: 
  1, A variable inside a normal code block x=3
      Variables within the main method x=1
     2, A variable inside a normal code block y=7
  */

2 construct the code block




// Building blocks: defined directly in the class and not added static A block of code for a keyword is called {} Construct the code block. The construction block is called when the object is created, every time the object is created, and the execution order of the construction block takes precedence over the class constructor. 

public class CodeBlock02{
  {
   System.out.println(" The first 1 The code block ");  
  }
  
  public CodeBlock02(){
    System.out.println(" A constructor ");
    }
    
    {
     System.out.println(" The first 2 Building blocks ");
    }
   public static void main(String[] args){
     new CodeBlock02();
     new CodeBlock02();
     new CodeBlock02();
      
  }
}  

/*
*
 Execution results: 
 The first 1 The code block 
 The first 2 Building blocks 
 A constructor 
 The first 1 The code block 
 The first 2 Building blocks 
 A constructor 
 The first 1 The code block 
 The first 2 Building blocks 
 A constructor 
*/

3 static code block




// Static code block : in java The use of static Keyword declaration block of code. The static block is used to initialize the class, and to initialize the properties of the class. Each static block of code only executes 1 Times. Due to the JVM A static block of code is executed when the class is loaded, so the static block executes before the main method. 
// If the class contains more than one static block of code, the following is used " The defined code is executed first, and the defined code is executed later " . 
// Note: 1  Static code blocks cannot exist in any method body. 2  Static code blocks do not have direct access to static instance variables and methods; they need to be accessed through the class's instance objects. 


class Code{
  {
   System.out.println("Code The structure of the block ");
  }
  
  static{
    System.out.println("Code Static block of code ");
    }
    
  public Code(){
    System.out.println("Code Method of construction ");
    }
  }
  
  
public class CodeBlock03{
   {
   System.out.println("CodeBlock03 The structure of the block ");  
   }
   
   static{
    System.out.println("CodeBlock03 Static block of code ");
    }
    
    public CodeBlock03(){
       System.out.println("CodeBlock03 Method of construction ");
      }
    
   public static void main(String[] args){
      System.out.println("CodeBlock03 The main method of ");
      new Code();
      new Code();
      new CodeBlock03();
      new CodeBlock03();
     }
  }
/*
CodeBlock03 Static block of code 
CodeBlock03 The main method of 
Code Static block of code 
Code The structure of the block 
Code Method of construction 
Code The structure of the block 
Code Method of construction 
CodeBlock03 The structure of the block 
CodeBlock03 Method of construction 
CodeBlock03 The structure of the block 
CodeBlock03 Method of construction 
*/

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: