Java Constructors Run parsing

Source: Internet
Author: User
Tags date class manager constructor execution
The function source code is as follows
Import java.util.*;
Class super1{
{
System.out.println ("Super1 OK");
}
Super1 ()
{
System.out.println ("3");
}
}

Class Employee extends super1{
private String name;
Private double salary=1500.00;
Private Date birthday;
Public Employee (String n,date DoB) {
System.out.println ("2");
Name=n;
Birthday=dob;
}
Public Employee (String N) {
This (n,null);
System.out.println ("4");
}
}

Class Manager extends employee{
{
System.out.println ("Manager OK");
}
Private String Department;
Public Manager (String n,string D) {
Super (n);
Department=d;
}
}

public class test1{
public static void Main (String args[]) {
New Manager ("Smith", "Sales");
}
}

The new Manager ("Smith", "Sales") invokes the procedure:
(1) binding constructor parameters. is actually the process of passing parameters
(2) To see if there is a this () statement. No. Although the constructor is not invoked using the This () statement, the step cannot omit
(3) Call the super () statement, at which point the program jumps to public Employee (String N).
(4) binding constructor parameter string n
(5) To see if there is this (). is available, the constructor public Employee (String n,date DoB) is executed
(6) binding constructor parameter string n,date DoB
(7) To see if there is a this () statement. No
(8) Execution of a super () statement with automatic system insertion: executive Super1 ()
(9) Perform an explicit initialization statement System.out.println ("Super1 OK");
(10) Executing the constructor statement System.out.println ("3");
(11) Perform an explicit initialization statement private double salary=1500.00;
(12) Executing the constructor statement System.out.println ("2") and executing the NAME=N;BIRTHDAY=DOB;
(13) Executing the constructor statement System.out.println ("4");
(14) Perform an explicit initialization statement System.out.println ("Manager OK");
(15) Executing constructor statement Department=d;

Some summary:
(1) The object was created by the new operator and was created before any constructors were executed
(2) The execution of the constructor is always "up": the constructor of the parent class is always executed first
(3) In the constructor, there is no this () statement by the super () statement. When there is no this (), either you write super () yourself, or the system automatically
With super ()
(4) An explicit initialization statement is always preceded by a constructor statement, but after the super () or This ()




Related Article

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.