Detailed analysis of the loading and object creation processes of Java classes

Source: Internet
Author: User

I believe that when we interview Java there will always be some companies to do written questions, and Java class loading and object creation process of knowledge points is also one of the common topics. The following is an example of a detailed analysis:

 package com.test;    

public class Parent {

int a = ten;
static int b = 11;
//Static code block
Static {
System.out.println ("Parent Static code block: b=" + b);
b++;
}
//code block
{
System.out.println ("Parent code block: a=" + a);
SYSTEM.OUT.PRINTLN ("Parent code block: b=" + b);
b++;
a++;
}
//parameterless constructor
Parent () {
System.out.println ("parent no parameter constructor: a=" + a);
SYSTEM.OUT.PRINTLN ("Parent no parameter constructor: b=" + b);
}
//Parameter constructor
parent (int a) {
System.out.println ("parent has parameter constructor: a=" + a);
SYSTEM.OUT.PRINTLN ("Parent has a parameter constructor: b=" + b);
}
//Method
Void function () {
System.out.println ("Parent function Run ...");
}
}

 package com.test;    

public class child extends Parent {

int x = ten;
static int y = 11;
//Static code block
Static {
System.out.println ("Child Static code block: y=" + y);
y++;
}
//code block
{
System.out.println ("Child code block: x=" + x);
SYSTEM.OUT.PRINTLN ("Child code block: y=" + y);
y++;
X + +;
}
//constructor
Child () {
System.out.println ("Child no parameter constructor: X" + x);
SYSTEM.OUT.PRINTLN ("Child no parameter constructor: y=" + y);
}
//Method
Void function () {
System.out.println ("Child function Run ...");
}

}

Package com.test;

public class Testpc {
public static void Main (string[] args) {

Child demo = new Child ();
Demo.function ();
System.out.println ("...... ..... ..... ..... ..... ..... ..... ..... ..... ..... ..... ..... ..... ..... ......
/* Child child = new Child ();
Child.function (); */
}
}


Results:

Parent Static code block: b=11
Child Static code block: y=11
Parent code block: a=10
Parent code block: b=12
Parent non-parametric constructor: a=11
Parent non-parametric constructor: b=13
Child code block: x=10
Child code block: y=12
Child non-parametric constructor: X11
Child non-parametric constructor: y=13
Child function Run ...
............................................................
Parent code block: a=10
Parent code block: b=13
Parent non-parametric constructor: a=11
Parent non-parametric constructor: b=14
Child code block: x=10
Child code block: y=13
Child non-parametric constructor: X11
Child non-parametric constructor: y=14
Child function Run ...





Detailed analysis of the loading and object creation processes of Java classes

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.