A topic that reflects the class initialization process of Java

Source: Internet
Author: User

The class initialization process for Java:

1. Static member variable of parent class, static statement block.

2. Static member variable of subclass, static statement block.

3. Ordinary member variables of the parent class, constructors.

4. Normal member variables of subclasses, constructors.

Note: If the following sentence is not understood, it's OK, look at one example below to understand ~

If the subclass overrides the member function of the parent class, and the member function is called in the constructor in procedure 3 above, then the member function of the subclass is called, and if the member function also contains a normal member variable that is not initialized in the subclass, the int type is initialized to 0 by default, and if the object type is Default is initialized to null.

The Java instance is as follows:

Abstract classBase { Public intAge = GetNumber (100); Static intSage = GetNumber (50); Static{System.out.println ("Base static Block"); }     PublicBase () {System.out.println (age); System.out.println ("Base Start");        Draw (); System.out.println ("Base End"); }    Static intGetNumber (intbase) {System.out.println ("Base.getnumber int" +base); returnBase; }     Public voidDraw () {System.out.println ("Base.draw"); }} Public classInitializeorderextendsBase { Public intAge = GetNumber (1001); Private int_radius = GetNumber (10); Static intSage = GetNumber (250); Static{System.out.println ("Subclass Static Block"); }     PublicInitializeorder (intradius) {_radius=radius;        System.out.println (age); System.out.println ("Initializeorder initialized"); }     Public voidDraw () {System.out.println ("Initializeorder.draw" +_radius); }         Public Static voidMain (string[] args) {NewInitializeorder (100); }}

Output Result:

Staticstatic  blockbase.getnumber int100base startinitializeorder.draw0 base endbase.getnumber int1001base.getnumber int101001initializeorder initialized

Pay particular attention to the red part of the above output!

A topic that reflects the class initialization process of Java

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.