Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the execution order of the java constructor

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces you what is the execution order of java constructor, the content is very detailed, interested friends can refer to, hope to be helpful to you.

1, when an object of a subclass is created without a static block, the constructor of the parent class-- > the constructor of the subclass (which produces the constructor of the object, if

No parameter executes a non-parameter constructor, if a parameter executes a parameter constructor)

There are now only two constructors in the parent class:

Father.java Father {public Father () {System.out.println ("I am the parent's no-parameter constructor");} public Father (String username) {System.out.println ("I am the parent's parameter constructor, and the parameter passed is +" + username) } public class SonDemo extends Father {public SonDemo () {System.out.println ("I am a no-parameter constructor of a subclass");} public SonDemo (String username) {System.out.println ("I am a parameterized constructor of a subclass with an argument of" + username ");} public void sys () {System.out.println (" I am a sys method of a subclass ") } public static void main (String [] args) {/ / the contents are explained below}}

①, the subclass creates an object using a no-parameter constructor:

Add the code to create the object in the main method of SonDemo:

SonDemo son = new SonDemo ();}

I am the parameterless constructor of the parent class

I am a parameterless constructor of a subclass.

②, the subclass uses a parametric constructor to create an object:

Add to the main method of SonDemo

SonDemo son = new SonDemo ("than you ma")

Then the result printed by the console is:

I am the parameterless constructor of the parent class

I am a parameter constructor of a subclass, and the parameter is than you ma

That is, when a subclass invokes a parameterless constructor to create an object, it first executes the parent's parameterless constructor before executing its own parameterized constructor.

③, create an object of the parent class in the subclass, using no parameters

SonDemo son = new SonDemo ("than you ma"); Father ff = new Father ()

Add to the main method of SonDemo

I am the parameterless constructor of the parent class

I am a parameterless constructor of a subclass.

I am the parameterless constructor of the parent class

The parameterless constructor of the parent class is called, and the parameterized creation object invokes the parameterized constructor.

Summary: when creating a subclass object, the constructor of the parent class will be called first, and then the corresponding constructor of the subclass will be called to create the object. When the subclass creates the parent object, the corresponding constructor of the parent class will be called directly.

two。 If there are static blocks in subclasses and parent classes; what will be the order of execution?

Answer, static blocks run before the constructor. Whether it is a subclass or a parent. When you create an object, its static blocks are loaded first.

Father.java public class Father {/ / static block static {System.out.println ("father static");} public Father () {System.out.println ("I am the parent class's no-parameter constructor");} public Father (String username) {System.out.println ("I am the parent class has a parameter constructor, the parameter passed is +" + username) }} SonDemo.java public class SonDemo extends Father {/ / static block static {System.out.println ("sonDemo static");} public SonDemo () {System.out.println ("I am a no-parameter constructor for a subclass") } public SonDemo (String username) {System.out.println ("I am the parametric constructor of the subclass with the argument" + username);} public void sys () {System.out.println ("I am the sys method of the subclass");} public static void main (String [] args) {SonDemo son = new SonDemo ();}}

The result of the ① program:

Father static sonDemo static, I am the parameterless constructor of the parent class. I am the parameterless constructor of the subclass.

Because an object of the parent class is created before the subclass object is created, and the static block is loaded before the main, the static block of the two classes is executed first.

Then execute the constructor.

②, what happens if you create only the objects of the parent class in the main in the subclass?

Print the results:

Father static sonDemo static I am the parameterless constructor of the parent class

Why are static blocks of subclasses loaded? Because we are testing in SonDemo, if we test in other classes, it will not print.

Conclusion: what we have said is an important point. Static blocks are executed before the constructor.

So much for sharing about the execution order of the java constructor. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report