Java member Initialization Order

Source: Internet
Author: User

 Packagecom.cwcec.test;classfu{intnum = 5; //Building Code Blocks{System.out.println ("Fu Constructor code ..." +num); Num=6; }     PublicFu () {System.out.println ("Fu constructor ..." +num);    Show (); }     voidShow () {System.out.println ("Show ..." +num); }    }classZiextendsfu{intnum = 9; //Building Code Blocks{System.out.println ("Zi Constructor code ..." +num); Num= 10; }     PublicZi () {System.out.println ("Zi constructor ..." +num); }    voidShow () {System.out.println ("Show ..." +num); }} Public classFielddemo { Public Static voidMain (string[] args) {NewZi (); }    } 
Output:Fu Constructor Code ... 5Fu Constructor ... 6show ... 0Zi Constructor Code ... 9Zi Constructor ... Ten

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////

classanimal{ PublicString name = "Ti";  Public Static intAge = 10; Static{System.out.println ("Animal Age:" +Age ); }     PublicAnimal () { age= 100; System.out.println ("Animal constructor ..."); }         Public voiddisplay () {System.out.println ("Name:" +name); }    }classDogextendsanimal{ Public Static intSize = 20; Static{System.out.println ("Dog Size:" +size); }         PublicDog () {size= 200; System.out.println ("Dog constructor ..."); }     Public Static voidsleep () {System.out.println ("Dog sleep ..."); }    } Public classsingle{ Public Static voidMain (string[] args) {Dog dog=NewDog (); }}

Summarize

1. The approximate order of implementation is as follows,

(1) In a class that does not exist: Initializes a static variable, performs a static initialization, and initializes a normal member variable (if there is an assignment statement), performs a normal initialization block--construction method

(2) In an inherited class: Initializes the parent class static member variable, runs the parent class static initialization block, initializes the subclass static member variable, runs the subclass static initialization block, and initializes the parent class instance member variable (if there is an assignment statement). Executes the parent class initialization block--the parent class constructor Method--initializes the subclass instance member variable (if there is an assignment statement) and the normal initialization block---subclass construction method.

Java member Initialization 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.