当前位置:网站首页>About constructive code blocks, static code blocks, and constructor execution order
About constructive code blocks, static code blocks, and constructor execution order
2022-06-13 01:28:00 【superJuice】
- Common code block : Method body of common method
- Building blocks of code : use
{}
The code of the package - Static code block : use
static{}
The code of the package
public class MethodTest {
private String tag;
static {
System.out.println(" Static code block ");
}
{
System.out.println(" Building blocks of code ");
}
public MethodTest(String tag) {
this.tag = tag;
System.out.println(tag + " Constructors ");
}
public void test() {
System.out.println(tag + " Common code block ");
}
public static void main(String[] args) {
MethodTest test1 = new MethodTest("test1");
test1.test();
MethodTest test2 = new MethodTest("test2");
test2.test();
}
}
Running results : Static code block
Building blocks of code
test1 Constructors
test1 Common code block
Building blocks of code
test2 Constructors
test2 Common code block
It can be seen from the running results that :
- The construction code block calls each time an object is created , Execute before constructor .
- Static code blocks are executed only once when the object is first created , And execute... Before constructing code blocks .
边栏推荐
- Binary tree traversal - recursive and iterative templates
- Detailed explanation of Joseph problem
- Exercise 5.14 input n strings, arrange them in alphabetical order and output them.
- Tkinter library installation
- Work and life
- Minimum score of one question per day
- Continue when the condition is not asked, execute the parameter you compare
- 他山之石:a16z 的 Web3 投资版图
- Plusieurs catégories de tests logiciels sont claires à première vue
- On February 26, 2022, the latest news of national oil price adjustment today
猜你喜欢
Rasa dialogue robot helpdesk (III)
Sliding window summary of TCP connections
Go JWT learning summary
Leetcode question brushing 02 linked list operation
Leetcode-19- delete the penultimate node of the linked list (medium)
Minimum score of one question per day
使用Pygame创建一个简单游戏界面
Plusieurs catégories de tests logiciels sont claires à première vue
Five classic articles worth reading (2)
Leetcode question brushing 06 bit operation
随机推荐
Memory learning book reference
Auto commit attribute of MySQL
Leetcode-9-palindromes (simple)
September 3, 2021 visual notes
Leetcode-12- integer to Roman numeral (medium)
MySQL performance optimization
Argparse command line passes list type parameter
Five classic articles worth reading (2)
Pysmb usage
leetode. 242. valid Letter heteronyms
Leetcode-13- Roman numeral to integer (simple)
Install pycharm process
5G工业网关在煤矿行业的应用优势
This of phaser3 add. sprite
[latex] insérer une image
leetcode. 541. reverse string II
Leetcode question brushing 03 stack
Crypto JS reports uglifyjs error
Introduction to common activation functions
使用Pygame创建一个简单游戏界面