In-depth understanding of Java program Execution order

Source: Internet
Author: User

Look at the code first:
Class Parent {
public static String P_staticfield = "Parent class-static variable";
Public String P_field = "Parent class-variable";
What are the following? Explain the following, not very clear
static {
System.out.println (P_staticfield);
System.out.println ("Parent class-static initialization block");
}
What's this for?
{
System.out.println (P_field);
System.out.println ("Parent class-Initialization block");
}
Public Parent () {
System.out.println ("Parent class-constructor");
}
}
public class Subclass extends Parent {
public static String S_staticfield = "sub-class-Static variable";
Public String S_field = "Subclass--variable";
Static initialization blocks
static {
System.out.println (S_staticfield);
System.out.println ("Subclass-static initialization block");
}
{
System.out.println (S_field);
System.out.println ("Subclass-Initialization block");
}
Public Subclass () {
System.out.println ("Subclass--constructor");
}
public static void Main (string[] args) {
New Parent ();
System.out.println ("-------------------");
New Subclass ();
System.out.println ("-------------------");
New Subclass ();
}
}
The running result of the program is:
Parent class--Static variable
Parent class--static initialization block
Subclass--Static variables
Subclass--Static initialization block
Parent class--variable
Parent class--initialization block
Parent class--constructor
-------------------
Parent class--variable
Parent class--initialization block
Parent class--constructor
Subclass--Variables
Subclass--Initialization block
Sub-class--constructor
-------------------
Parent class--variable
Parent class--initialization block
Parent class--constructor
Subclass--Variables
Subclass--Initialization block
Sub-class--constructor

classA { PublicA () {System.out.println ("A method of construction"); }     Public Static intj =print ();  Public Static intprint () {System.out.println ("A print"); return521; }} Public classBextendsA { PublicB () {System.out.println ("B's Construction method"); }     Public Static intK =print ();  Public Static intprint () {System.out.println ("B print"); return522; }     Public Static voidMain (string[] args) {System.out.println ("Main Start"); b b=NewB (); System.out.println ("------------------------"); A A=NewB (); }}

Output:

A Print
B Print
Main start
The construction method of a
Construction method of B
------------------------
The construction method of a
Construction method of B

In-depth understanding of Java program Execution order

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.