On the loading order of parent class and subclass in Java _java

Source: Internet
Author: User
Copy Code code as follows:

Class Parent {
static variables
public static String P_staticfield = "Parent class-static variable";
Variables (in fact, this is better for objects with this, such as writing a class-specific instance)
  
If this variable is placed behind the initialization block, it will be an error, because you are not initialized at all.
Public String P_field = "Parent class-variable";
Static initialization block
static {
System.out.println (P_staticfield);
System.out.println ("Parent class-static initialization block");
}
Initializing blocks
{
System.out.println (P_field);
System.out.println ("Parent class-Initialization block");
}
Construction device
Public Parent () {
System.out.println ("Parent class-constructor");
}
}
public class Subclass extends Parent {
static variables
public static String S_staticfield = "Subclass--static variable";
Variable
Public String S_field = "Subclass--variable";
Static initialization block
static {
System.out.println (S_staticfield);
System.out.println ("Subclass--static initialization block");
}
Initializing blocks
{
System.out.println (S_field);
System.out.println ("subclass--initialization block");
}
Construction device
Public Subclass () {
Super ();
System.out.println ("Subclass--constructor");
}
Program entry
public static void Main (string[] args) {
System.out.println ("*************in main***************");
New Subclass ();
System.out.println ("*************second subclass***************");
New Subclass ();
}
}

Output results
Parent class--Static variables
Parent class--static initialization block
Subclasses--Static variables
Subclass--Static initialization block
In main***************
Parent class--variable
Parent class--Initialize block
Parent class--builder
Subclasses--variables
Subclass--Initialize block
Subclass--Builder
Second subclass***************
Parent class--variable
Parent class--Initialize block
Parent class--builder
Subclasses--variables
Subclass--Initialize block
Subclass--Builder

Results Analysis:
Obviously after loading the main method, the static variable executes regardless of the parent class or subclass, and then the normal variables and constructors of the parent class and subclass. This is because, when you want to create a subclass of this object, found that this class requires a parent class, so the. Class of the parent class is loaded in, then initializes its normal variable and initializes the code block, and finally its constructor, and then it can start the work of the subclass, and the handle class. Class is loaded in, doing the subclass work.

In addition, in the Java neutron class there is a default constructor called Super () that invokes the parent class, and when there is only a default constructor
Java did it for you, we can do an experiment, if the default constructor is commented out in the parent class, plus a parameter constructor, if
Subclass without Super (argument), a syntax error will be reported at this time
If we annotate all the contents of the main method, you will find that only the output

Parent class--Static variables
Parent class--static initialization block
Subclasses--Static variables
Subclass--Static initialization block
The others will not be exported. What's the reason? Also to study

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.