Java subclass instance initialization process

Source: Internet
Author: User

Subclass instantiation involves two steps:

<1>. class-relatedStatic contentInitialization;

* Parent class and Child class:

 1. Static attributes of the parent class;

2. Static block of the parent class;

3. Static attributes of subclass;

4. Static blocks of child classes;

<2>.Instance contentInitialization;

* Call construction sub-: first parent class and then subclass: instance fields and blocks are instantiated before a sub-call is constructed

5. common attributes of the parent class;

6. Common blocks of the parent class;

7. constructor of the parent class;

8. common attributes of sub-classes;

9. Common blocks of sub-classes;

10. constructor of subclass;

 

Example:

Package basic; public class clzinit {public static void main (string [] ARGs) {New son () ;}} Class Parent {protected int n = 5; protected static int M = 5; static {M = m * 2; system. out. println ("parent class static block call; M =" + M) ;}{ n = N * 2; M = m * 2; system. out. print ("parent class common block call;"); system. out. print ("n =" + n); system. out. println ("m =" + M);} public parent () {This. N = N * 10; M = m + 10; system. out. print ("parent class constructor; n =" + n); system. out. println ("m =" + M) ;}} class son extends parent {private int Sn = 3; Private Static int Sm = 3; static {M = m + 2; sm = Sm + 2; system. out. println ("subclass static block call; M =" + M) ;}{ n = n + 2; Sn = Sn + 2; M = m + 2; system. out. println ("subclass common block call;"); system. out. print ("n =" + n); system. out. print ("Sn =" + SN); system. out. println ("m =" + M);} public son () {This. N = N + 10; Sn = Sn + 10; M = m + 10; system. out. print ("subclass constructor; n =" + n); system. out. println ("m =" + M );}}

Looking at the output results, we can see the clues:

Parent class static block call; M = 10 subclass static block call; M = 12 parent class common block call; n = 10 m = 24 parent class constructor; N = 100 m = 34 subclass common block call; n = 102sn = 5 m = 36 subclass constructor; n = 112 m = 46

Memo. Thank you for your correction!

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.