Home > Java > javaTutorial > body text

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

黄舟
Release: 2017-08-22 09:59:50
Original
1772 people have browsed it

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();
 }
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template