Java Parent class child class member variable, static variable, constructor creation sequence

Source: Internet
Author: User

In the order of the parent class subclass, member variables, static variables, constructor creation in Java, this type of question is often asked in the interview.

So test it with the following code:





 Public classTest { Public Static voidMain (String args[]) {Father F=NewSon (); }}classFather { PublicFather () {System.out.println ("Father Construct"); }    Static intFatherstatic=printfatherstatic (); intFather=Printfather (); Static intprintfatherstatic () {System.out.println ("Print in Father static Mamber"); return0; }    Static intPrintfather () {System.out.println ("Print in Father Class Mamber"); return0; }}classSonextendsFather { PublicSon () {System.out.println ("Son Construct."); }    Static intSonstatic=printsonstatic (); intson=Printson (); Static intprintsonstatic () {System.out.println ("Print in son static Mamber"); return0; }    Static intPrintson () {System.out.println ("Print in Son class Mamber"); return0; }}



The result of the operation is:

Print in Father Static Mamber
Print in son static Mamber
Print in Father class Mamber
Father Construct
Print in Son class Mamber
Son Construct

To summarize, draw the following conclusions:

The Order of creation is: the static variable of the parent class--The child class, the member of the parent class, the parent class, the constructor method of the child class, member variable-

In general, create a static method first, then create the member variables and constructor methods of the parent class, and then create the member variables and construction methods of the child class

Java Parent class child class member variable, static variable, constructor creation sequence

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.