[Thinking in Java] note when writing constructor: Avoid calling other non-private methods, thinkingprivate

Source: Internet
Author: User

[Thinking in Java] note when writing constructor: Avoid calling other non-private methods, thinkingprivate

I recently reviewed Thinking in Java and found an exciting knowledge vulnerability. I have to share it with you.

In the previous article "Java class initialization process", the initialization sequence is incomplete. The actual initialization process is:

However, we know that when the Sub class inherits the Sup of the parent class and overwrites the draw () method of the parent class, even if we transform to a parent class (Sup sup = new Sub (), when we call sup. the draw () method actually calls the draw method of Sub. There is a pitfall here!

If the draw () method is called in the constructor of the parent class, logically, we assume that the draw () method of the parent class is called. In fact, even within the constructor of the parent class, the Java compiler calls the draw () method of the subclass.

Here is an example:

  

Public class Glyph {Glyph () {System. out. println ("Glyph before draw ()"); draw (); // logically, the draw () of this class should be called. However, the result is not System. out. println ("Glyph after draw ()");} void draw () {System. out. println ("Glyph. draw () ") ;}} public class RoundGlyph extends Glyph {private int radius = 1; public RoundGlyph (int r) {radius = r; System. out. println ("RoundGlyph. roundGlyph (), radius = "+ radius) ;}@ Override void draw () {System. out. println ("RoundGlyph. draw (), radius = "+ radius) ;}} public class PolyConstructors {public static void main (String [] args) {new RoundGlyph (5 );}}

The printed result is:

Glyph before draw ()
RoundGlyph. draw (), radius = 0
Glyph after draw ()
RoundGlyph. RoundGlyph (), radius = 5

Look at the second line of printing. Logically, the print Should Be Glyph. draw (), but it is overwritten by the quilt class. In addition, even if RoundGlyph. draw () and radius = 0 are printed, the radius should be equal to 1! No. According to the class initialization order, the constructor of the parent class Glyph is initialized first, and then the radius of the member variable of RoundGlyph is initialized. The reason for radius = 0 is that the Java class loading mechanism is in the preparation stage, that is, before the user initializes the variable, the variable has been initialized to 0. For the class loading mechanism of Java virtual machine, let's take a look at Chapter 7th in the book "deep understanding of Java virtual machines.

This kind of bug is hard to find, but it will damage the program itself and let us forget the bug.

Therefore, when initializing the constructor, we should try to make it as simple as possible, and avoid calling other public non-constructor In the constructor as much as possible (private methods can be called, because it cannot be inherited ).

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.