Home  >  Article  >  Java  >  Differences between ordinary code blocks, constructed code blocks, and static code blocks in Java and code examples

Differences between ordinary code blocks, constructed code blocks, and static code blocks in Java and code examples

高洛峰
高洛峰Original
2017-01-18 15:14:111220browse

The difference between ordinary code blocks, constructed code blocks, static code blocks and code examples in Java

//Execution order: (Priority from high to low.) Static code block>mian method>Construction Code blocks > Constructors.

The static code block is only executed once. The construction code block is executed every time an object is created.

1 Ordinary code block

//普通代码块:在方法或语句中出现的{}就称为普通代码块。普通代码块和一般的语句执行顺序由他们在代码中出现的次序决定--“先出现先执行”
public class CodeBlock01{
   public static void main(String[] args){
      
      {
       int x=3;
       System.out.println("1,普通代码块内的变量x="+x); 
      }
       
      int x=1;
      System.out.println("主方法内的变量x="+x);
       
      {
        int y=7;
        System.out.println("2,普通代码块内的变量y="+y); 
      }
     }
  }
   
  /*
  运行结果:
  1,普通代码块内的变量x=3
     主方法内的变量x=1
     2,普通代码块内的变量y=7
  */

2 Constructed code block

//构造块:直接在类中定义且没有加static关键字的代码块称为{}构造代码块。构造代码块在创建对象时被调用,每次创建对象都会被调用,并且构造代码块的执行次序优先于类构造函数。
 
public class CodeBlock02{
  {
   System.out.println("第一代码块"); 
  }
   
  public CodeBlock02(){
    System.out.println("构造方法");
    }
     
    {
     System.out.println("第二构造块");
    }
   public static void main(String[] args){
     new CodeBlock02();
     new CodeBlock02();
     new CodeBlock02();
       
  }
} 
 
/*
*
执行结果:
第一代码块
第二构造块
构造方法
第一代码块
第二构造块
构造方法
第一代码块
第二构造块
构造方法
*/

3 Static code block

//静态代码块:在java中使用static关键字声明的代码块。静态块用于初始化类,为类的属性初始化。每个静态代码块只会执行一次。由于JVM在加载类时会执行静态代码块,所以静态代码块先于主方法执行。
//如果类中包含多个静态代码块,那么将按照"先定义的代码先执行,后定义的代码后执行"。
//注意:1 静态代码块不能存在于任何方法体内。2 静态代码块不能直接访问静态实例变量和实例方法,需要通过类的实例对象来访问。
 
 
class Code{
  {
   System.out.println("Code的构造块");
  }
   
  static{
    System.out.println("Code的静态代码块");
    }
     
  public Code(){
    System.out.println("Code的构造方法");
    }
  }
   
   
public class CodeBlock03{
   {
   System.out.println("CodeBlock03的构造块"); 
   }
    
   static{
    System.out.println("CodeBlock03的静态代码块");
    }
     
    public CodeBlock03(){
       System.out.println("CodeBlock03的构造方法");
      }
     
   public static void main(String[] args){
      System.out.println("CodeBlock03的主方法");
      new Code();
      new Code();
      new CodeBlock03();
      new CodeBlock03();
     }
  }
/*
CodeBlock03的静态代码块
CodeBlock03的主方法
Code的静态代码块
Code的构造块
Code的构造方法
Code的构造块
Code的构造方法
CodeBlock03的构造块
CodeBlock03的构造方法
CodeBlock03的构造块
CodeBlock03的构造方法
*/

Thank you for reading, I hope it can help everyone, thank you for your support of this site!

For more articles on the differences between ordinary code blocks, constructed code blocks, static code blocks and code examples in Java, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn