The Java initialization sequence and the potential problems of the constructor methods that might be thrown by this.

Source: Internet
Author: User

"A particularly effective rule when designing the builder is to put the object in a ready state in as simple a way as possible, and avoid calling any methods if possible." The only methods that are safe to invoke in the builder are those that have a final attribute in the underlying class (also for private methods, which automatically have a final attribute). These methods cannot be overwritten, so there is no such potential problem. ”

Excerpt from: (United States) Dr. Eckel Chen Haopeng translation. "Java Programming ideas".

So what is the underlying problem? Take a look at the code:

Abstract class Glypy {    static int radius = 2;    abstract void Draw ();    Glypy () {        System.out.println ("Glyph () before Draw ()");        Draw ();        System.out.println ("Glypy () after draw ()");}    } Class Roundglyph extends Glypy {    int radius = 1;    Roundglyph (int r) {        radius = R;        System.out.println ("roundglyph.roundglyph,radius=" + radius);    }    void Draw () {        System.out.println ("Roundglyph.draw (), radius=" + radius);}    } public class Polyconstructors {public    static void Main (string[] args) {        new roundglyph (5);}    }

Output Result:

Glyph () before Draw () Roundglyph.draw (), Radius=0glypy () after draw () roundglyph.roundglyph,radius= 5

This begs the question of an initialization order, and for an inherited situation, it is clear that the parent class is initialized first, and then the child class. Coupled with static properties, static blocks, and non-static properties, the order of construction methods.

So if there are both inheritance and static properties static and so on.

Static attributes (post-father subclass)-static blocks (after the Father Class)--Non-static attributes (after the late father subclass)--construction method (after the late father subclass)


The Java initialization sequence and the potential problems of the constructor methods that might be thrown by this.

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.