Execution order of Java construction methods

Source: Internet
Author: User

1. If no construction method is defined inside the class, a default parameterless construction method is added.

Class thisclass{}

By default, the implicit addition of an parameterless construction method is equivalent to

Class thisclass{public ThisClass () {}}
2. The execution order of the construction method.

(1) If the constructor of the parent class is not explicitly called in the construction method, and no overloaded construction method is called for this class, the system will add the call to the parent class without a parameter constructor by default when the constructor method is executed.

Public ThisClass () {}
By default, the implicit addition of the parent class is a non-parametric construction method, equivalent to
Public ThisClass () {super ();}
(2) If the overloaded construction method of this class is called in the constructor method in the first statement, then the overloaded construction method of the class is executed, and the rules are executed as above.
Public ThisClass () {this (3);} public thisclass (int x) {}

When calling ThisClass's parameterless construction method, because its first sentence calls the overloaded construction method of this class, the program enters the overloaded method with the parameter, the method conforms to (1), so the method of constructing the parent class is implicitly added.

Note:
(1) The constructor method that explicitly calls the parent class and other overloaded constructor methods that call this class require that the first statement of the constructed method must be placed, so the constructor of the parent class cannot be called in the same constructor, and other overloaded construction methods of this class are called.


(2) When the parent class constructor method is not explicitly called in the subclass constructor method, the system calls the parent class without a parameter constructor by default, and if the parent class does not have a parameterless construction method, a compilation error occurs.

Attached: When you new an object, the order in which the code is executed

public class Test {public static void main (string[] args) {System.out.println ("xxxxx for the first time new B object xxxxx"); new B (); System.out.println ("xxxxx second new B object xxxxx"); new B ();}} Class A {{System.out.println ("Free block of Parent Class A");} A () {System.out.println ("parent Class A constructs method \ n");} D = new D ("the member variable of parent Class A is initialized \ n"); static {System.out.println ("static free block of parent Class A");} Static d dd = new D ("The statically member variable of parent Class A is initialized \ n"); The Class B extends A {static D ddd = new D ("The static member variable of subclass B is initialized"); static {System.out.println ("Static free block of subclass b \ n");} D dddd = new D ("member variable of subclass B is initialized"); B () {super (); System.out.println ("Construction method of Subclass B");} {System.out.println ("Free block of subclass b \ n");}} Class D {d (String str) {System.out.println (str);}}

Execution Result:

XXXXX for the first time the new B object xxxxx Static free block of parent Class A static member variable of parent Class A a static member variable of subclass B is initialized subclass B static free block parent Class A's free block parent Class A member variable is initialized parent Class A constructor method subclass B's member variable is initialized subclass B's free Block subclass B constructor method xxxxx The second time the new B object xxxxx the free block of the parent class A member variable of the parent class A is initialized the constructor method of the parent class A, the member variable of subclass B is initialized subclass B's free block subclass B's Construction method

Execution order of Java construction methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.