Notes about the Java Builder

Source: Internet
Author: User

1. Constructor initialization process that does not involve inheritance and polymorphism

When new A () is present in the program, or when code such as a static method of Class A is used (declaring a Class A object does not count) the Java Virtual Opportunity first loads the Class A and then performs a static initialization, the static initialization initializes all static member variables by default, that is, the base data type is initialized to 0. The reference data type is initialized to null. It is then initialized by the criteria for the initialization and declaration order of static variables and static zones. The static initialization performed at this time is performed only this time and is no longer executed after execution.

Then, if new A A () is present, the object is allocated storage space, then the default initialization is performed on all non-static member variables, and then the object class is loaded (if it is not loaded) and then the object is initialized. It then returns to the inside of Class A, initializing it in the order of member variables and non-static zone declarations. Finally, the constructor is executed to complete the initialization process.

2. When it comes to inheritance and polymorphism, initialization becomes a little more complicated.

involves inheritance, if the Class A has one or more parent classes, the static initialization steps are not changed, but when you create a new object, after the default initialization, the parent class is looked up to the object class, and then the object class and other parent classes are initialized to the next level.

Polymorphism is involved, and if a function that involves polymorphism is called in the non-static code block of the parent class or in the constructor of the parent class, then the function is a corresponding overridden function of Class A.

Class Glyph{{draw ();//Call}void Draw () {System.out.println ("Glyph Draw ()") of the subclass;} Public Glyph () {System.out.println ("Glyph () before Draw ()");d Raw ();//calls the subclass's System.out.println ("Glyph () after draw ()" ) ;}} public class Roundgroph extends glyph{private int radius = 1; Roundgroph (int i) {radius = i; System.out.println ("roundgroph.grougph (). radius=" + radius);} void Draw () {System.out.println ("Roundgroph.draw (). radius=" + radius);} public static void Main (String args[]) {Roundgroph a  = new Roundgroph (22);}}

This occurs because of dynamic binding. The reason why Java is polymorphic is due to <a href= "http://www.cnblogs.com/yyyyy5101/archive/2011/08/02/2125324.html" > Dynamic binding </a>.

The above code is an explanation of the phrase "thinking in Java"

If the constructor is simply a step in building an object, and the class to which it belongs is derived from the class to which the constructor belongs, then the exported part is still not initialized at the moment the current constructor is being called. However, a dynamically bound method call will go deep inside the inheritance hierarchy and invoke the method of exporting the class. If we do this in a constructor, then we might call a method, and the member used by this method may not have been initialized yet-this will certainly Shang the curse.

the Roundgroph in the above code is a subclass of glyph. As described above for an initialization process with an inheritance relationship, when new Roundgroph (22) is encountered, the storage space is allocated and the member of the Roundgroph is initialized by default, then initializes the The Roundgroph object, which calls draw () during initialization of the glyph, is dynamically bound and invokes the Roundgroph draw () if the roundgroph is not fully initialized, if draw () is using uninitialized subclass member variables, it can lead to inexplicable errors, that is, "thingking in Java" in the Shang of the curse.

Thinking in Java note, if there is a wrong place, also look at ^_^

Notes about the Java Builder

Related Article

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.