Home  >  Article  >  Java  >  Three cases, interpreting static code blocks and constructed code blocks

Three cases, interpreting static code blocks and constructed code blocks

巴扎黑
巴扎黑Original
2017-06-26 09:30:081591browse

1. A brief summary of static code blocks and non-static code blocks

1. Static code blocks:

Static code blocks are used when a class is It is called first when loading (memory) and is executed once. Static blocks are often used to initialize class attributes. It will be called before executing the loading of class.

2. Non-static code block:

The non-static code block is the first to be called when the object of the class is created and loaded (memory). Every time an object is created, that is, every time When an object is loaded, non-static code blocks are executed once. It will be called before executing the loading of class object.

3. Static methods and non-static methods: are executed when called. Static methods belong to classes and can be called after loading the class; non-static methods belong to objects and non-static methods can be called after loading the object.

2. Related example demonstrations

 1 public class Cc { 2     public static void main(String[] args) { 3         test p=new test(); 4     } 5 } 6  7 class test { 8     test() {  //在类对象创建的时候就开始执行。简称创建即执行。 9         System.out.println("这是构造函数");10     }11 12     {  //构造代码块,执行在构造函数之前,每当在创建类对象的时候都执行。13         System.out.println("这是构造代码块");14     }15     16     static {  //静态代码块,最早执行,且值执行一次。17         System.out.println("这是静态代码块");18     }19 }

Print results:

Demonstration time What happens when called twice is the printed result. Deepen your understanding of static code blocks.

 1 public class Ccc { 2 public static void main(String[]args) 3 { 4     MyTest p=new MyTest(); 5     MyTest p2=new MyTest();//两次调用 6 } 7 } 8 class MyTest{ 9     10     MyTest(){11         System.out.println("这是构造函数");12     }13     14     {15         System.out.println("这是构造代码块");16     }17     18     static{19         System.out.println("这是静态代码块");20         21     }22 }

The print result is as follows:

Introducing an example with parameters:

 1 class Cccc { 
 2     public static void main(String[] args) { 
 3         new Person(77);
 4         new Person();
 5     }
 6 } 
 7  
 8 class Person { 
 9     
 10     Person() {
 11         System.out.println("a");
 12     }
 13 
 14     
 15     Person(int x) {  //注意带参数了!!
 16         System.out.println(x);
 17     }
 18 
 19     static {
 20         System.out.println("b");
 21     }
 22 
 23     {
 24         System.out.println("c");
 25     }
 26 }

Print results:

The above is the detailed content of Three cases, interpreting static code blocks and constructed 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
Previous article:Selector of JAVA-5NIONext article:Selector of JAVA-5NIO