当前位置:网站首页>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
边栏推荐
- Composition and simple classification of IP addresses
- Downloading sqlserver versions (2016-2019)
- ABCD identifier of SAP mm initial cycle count
- Preliminary sequencing problem
- Flowable refactoring process editor to obtain user information
- Chapter IV open source projects and deployment
- What are the advantages and difficulties of introducing AI into ISP Technology
- Customization of openfeign
- [JS reverse hundreds of cases] the login of a HN service network is reverse, and the verification code is null and void
- Easygbs solution to video playback failure caused by tcp/udp configuration problems
猜你喜欢
![[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]

Hierarchical attention graph convolution network for interpretable recommendation based on knowledge graph
![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]

1058 multiple choice questions (20 points)

Svn local computer storage configuration

【贪心】leetcode991. Broken Calculator
![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 duty-free industry in Hainan Province in 2021: the implementation of the new policy makes the duty-free market in Hainan more "prosperous" [figure]
![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]

Gakataka student end to bundle Version (made by likewendy)
随机推荐
Ultra detailed Apache and PHP installation tutorial windows (2022.1)
Implementation process of the new electronic amplification function of easycvr video fusion cloud platform
How to install redis version 5.0.8 on the pagoda panel
Generate PDF417 code in batch through TXT file
Configuring multi cluster management using kubectl
Enterprise official website applet building tutorial
CFs of cifs/smb protocol is mounted on win10/2019. Error 1272 is reported. The security policy prevents unauthenticated guest access
Auto rename when uploading pictures on WordPress media
Nezha panel modifies logo, small icon and other information
Initialize MySQL Gorm through yaml file
How to store easydss version 3.0 video files in other free disks?
Simply use the pagoda to build WordPress
Account MFA usage scheme
Application of map function in JS
What should be prepared to develop the company's official website?
Even if you don't learn gradle, these common development operations are worth mastering
Decentralized networks are not decentralized
Uploading logs using loghub log4j appender
JS remove first character of string
Minecraft server technology explanation 𞓜 teach you how to get to the ashes from Xiaobai -- server technology explanation