Java Supplements (i): An analysis of the instantiation order and trap of Java subclass and parent class

Source: Internet
Author: User

This article mainly introduces the sequence and pitfalls of variable instantiation of subclasses and parents commonly used in Java, and discusses this problem in conjunction with an Android instance. You should try to avoid this trap in future programming.

First look at the following code:

Defines a virtual class Server.java

Package Org.yanzi.test1;public abstract Class Server {private static final int default_port = 900;public Server () {//TODO auto-generated constructor Stubint port = Getport (); System.out.println ("port =" + Port + "Default_port =" + Default_port);} protected abstract int getport ();}

Then define a subclass Simpleserver.java

Package Org.yanzi.test1;public class Simpleserver extends Server {private int mport = 100;public simpleserver (int port) {/ /TODO auto-generated constructor stubthis.mport = port;} @Overrideprotected int Getport () {//TODO auto-generated method Stubreturn mport;}}

Test code:

Package Org.yanzi.test1;public class Test1 {/** * @param args */public static void main (string[] args) {//TODO Auto-gener Ated method Stubserver s = new Simpleserver (600);}}

Test results:

Port = 0 Default_port = 900

In the test code, passed a number of 600, we hope Getport get is also 600, but unfortunately get a big 0!!! This problem occurs because of the ambiguity in the order of the instantiation of the Java subclass and the parent class, and the following is the correct order:

1, the new one simpleserver,simpleserver the constructor to receive the number of parameters 600;

2, initializes the static variable of the parent Class Server, Default_port assigns the value to be 900;

3, in order to instantiate the subclass, first instantiate its parent class server. The subclass has a reference to the constructor that contains the super method by default, that is, the non-participating constructor that invokes the parent class . So it's an int port = Getport (); This sentence calls the Getport method of the subclass. The Getport method of the subclass returns Mport, at which point the mport has not yet been assigned, so it is still 0. This is the reason for getting big 0!!!

4, after the initialization of the parent class is complete, initializes the instance variable of the subclass, Mport assigns the value 100;

5, the constructor of the sub-class is run, Mport assignment value 600;

6, sub-class instantiation complete, the object has been created!

Come to the truth and make a test. Mport The instance variable in Simpleserver into a static variable such as the following:

Package Org.yanzi.test1;public class Simpleserver extends Server {private static int mport = 100;public simpleserver (int p ORT) {//TODO auto-generated constructor stubthis.mport = port;} @Overrideprotected int Getport () {//TODO auto-generated method Stubreturn mport;}}
Test results:

Port = Default_port = 900

The order in which it runs is:

1. First step ibid., simpleserver receive construction parameters 600

2. Initialize the static code block of the parent class, which of course contains static variables, and then initialize the child class's static variables

3. Initialize the non-static code of the parent class, including non-static variables, etc.;

4. Run the constructor of the parent class;

5. Initializing non-static code for subclasses

6. Run the subclass constructor.

At this point, the conclusion is that the simpler the constructor, the better, do not do too much in the constructor function. Go back and look at Zajia's previous article:Android itself defines UI traps: Layoutinflater.from (). Inflate () must not work in the parent class or virtual class assuming that the Initview () is placed in the parent class, the subclass Layout_ The ID is also 0 when it is used. So even if you want to use it, make it into a static type.

This article is about writing high-quality code: 151 Suggestions for improving Java, link 1, Link 2

Java Supplements (i): An analysis of the instantiation order and trap of Java subclass and parent class

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.