Home >Java >javaTutorial >Java examples explain the execution order of ordinary code blocks and static code blocks

Java examples explain the execution order of ordinary code blocks and static code blocks

黄舟
黄舟Original
2017-08-22 09:59:501816browse

The following editor will bring you an article on the execution sequence of Java ordinary code blocks and static code blocks (explanation with examples). The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

is as follows:


##

class B {
 public B() {
  super();
  System.out.println("构造器B");
 }
 {
  System.out.println("普通的代码块B");
 }
 static{
  System.out.println("静态代码块B");
 }
 
}
public class ClassA extends B {

 public ClassA() {
  super();
  System.out.println("构造器A");
 }
 {
  System.out.println("普通的代码块A");
 }
 static{
  System.out.println("静态代码块A");
 }
 public static void main(String[] args) {
  ClassA a=new ClassA();
 }
}

The running results are as follows:

Static code block B

Static code block A
Ordinary code block B
Constructor B
Ordinary code block A
Construction Container A

can see that the static code block is executed first, then the ordinary code block of the parent class, the parent class constructor, then the ordinary code block of the subclass, and the subclass constructor Device

The above is the detailed content of Java examples explain the execution order of ordinary code blocks and static code blocks. For more information, please follow other related articles on 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