当前位置:网站首页>Static code block, code block, constructor execution order
Static code block, code block, constructor execution order
2022-06-23 03:33:00 【Feng 210】
The first is the parent class
public class Father {
String name;
int age;
public Father(String name, int age) {
this.name = name;
this.age = age;
System.out.println(" The parent class has a parametric constructor ");
}
public Father(){
System.out.println(" Parent class parameterless constructor ");
}
static {
System.out.println(" Parent static code block ");
}
{
System.out.println(" The parent class constructs the code block ");
}
public void test1(){
{
System.out.println(" Parent normal code block ");
}
}
}Subclass
public class Son extends Father {
public Son(String name, int age) {
super(name, age);
System.out.println(" Subclass parameterized constructor ");
}
public Son() {
System.out.println(" Subclass parameterless constructors ");
}
static {
System.out.println(" Subclass static code block ");
}
{
System.out.println(" Subclass construct code block ");
}
public void test2(){
{
System.out.println(" Subclass common code block ");
}
}
}Test class
public class Test {
public static void main(String[] args) {
Son son=new Son();
}
}
Take a look at the results of the run

We can see the execution sequence of the parent class static code block
Parent static code block
Subclass static code block
The parent class constructs the code block
Parent class parameterless constructor
Subclass construct code block
Subclass parameterless constructors
We can see that a total of 6 results , So why do we just create an initialization Son, This will happen ?
First : We create a Son object , Called Son The parameterless constructor of , And ours Son The parameterless constructor will automatically call the parameterless constructor of the parent class .
amount to

Add : Even when we create a class , No write constructor ,java We will also add a parameterless constructor by default , If you don't write any constructors .
summary :
Parent static code block ( Static variables > A static block ) > Static code block of subclass > The parent class constructs the code block 、 Construction method > Construction code block of subclass 、 Construction method
边栏推荐
- 2022-01-25: serialize and deserialize n-ary tree. Serialization means that a
- Account MFA usage scheme
- Detailed discussion on modular architecture design of MCU firmware
- Pre and post processing of pytest
- Interrupt array Foreach method [js implementation]
- Get method of fetch request and data of formdata type submitted by post
- QUIC or TCP
- Evolution of cloud firewall products
- An implementation of warning bombing
- "Tencent conference": how to operate and maintain efficiently in the face of exponential growth of business?
猜你喜欢
![Analysis on demand and market scale of China's steamed stuffed bun industry in 2020 [figure]](/img/4b/dd272f98b89a157180bf68570d2763.jpg)
Analysis on demand and market scale of China's steamed stuffed bun industry in 2020 [figure]

Svn local computer storage configuration
![Analysis of the number of urban residents covered by basic medical insurance, their treatment and medical treatment in other places in China in 2021 [figure]](/img/81/4d3cb059f700dd9243645e64023be7.jpg)
Analysis of the number of urban residents covered by basic medical insurance, their treatment and medical treatment in other places in China in 2021 [figure]
![[quick view] Analysis on the development status and future development trend of the global and Chinese diamond cultivation industry in 2021 [figure]](/img/f1/972a760459a6d599b5681aa634df09.jpg)
[quick view] Analysis on the development status and future development trend of the global and Chinese diamond cultivation industry in 2021 [figure]
![Analysis of China's integrated circuit industry chain in 2021: huge downstream market demand [figure]](/img/de/d73805aaf4345ca3d2a7baf85aab8d.jpg)
Analysis of China's integrated circuit industry chain in 2021: huge downstream market demand [figure]

Analysis on the development of China's satellite navigation industry chain in 2021: satellite navigation is fully integrated into production and life, and the satellite navigation industry is also boo

Jmeter- (V) simulated user concurrent login for interface test

One of the touchdesigner uses - Download and install

【二分】leetcode1011. Capacity To Ship Packages Within D Days
![Analysis on development history, industrial chain, output and enterprise layout of medical polypropylene in China in 2020 [figure]](/img/28/ebfc25ec288627706e15a07e6bdb77.jpg)
Analysis on development history, industrial chain, output and enterprise layout of medical polypropylene in China in 2020 [figure]
随机推荐
About SSL certificates
Application of map function in JS
Drill down into handler, looper, messagequeue
How to batch generate ean14 barcode through TXT file
Why not use math random() ?!
Tencent released the global DDoS threat report in 2021: DDoS threat has become the preferred extortion method for criminal gangs
C. Differential Sorting
Learning record -- superficial understanding of unity decoupling
Postman calls relevant interfaces of CLS API 3.0
RTOS system selection for charging point software design
Engineer culture: should the company buy genuine software
How to make distribution box label
Using promise to process asynchronous operations
[burning] Tencent cloud high tech computing platform HTPC cloud elastic cluster release!
Composition and simple classification of IP addresses
[advanced Android] kotlin delegate attribute
Free upgrade of 2-core 2GB for old generation 1-core 2GB machines below standard S5 and SA2
Build a weather forecast applet using a widget
Pre and post processing of pytest
Bi skills - authority control