Initialize from Java class to see Code optimization

Source: Internet
Author: User

Java The class initialization order may be caused by the Bug


   Recently encountered problems in programming , class member initialization process Everyone knows, is the basic knowledge, but some places are very subtle, re-learn

To improve the quality of your code.


Describe the scenario that you are experiencing first:

sub-class constructors call the Span style= "font-family:liberation serif,times New roman,serif" >super (), The subclass is then called in the parent class builder with @overwrite method, subclass in Overwrite in the method of

member Assignment, Log the output succeeds in assigning a value to the sub-class New Finish, Log Print Discovery Section the value of the member variable is missing.



Print log found the list data is missing, theint value is still , as follows:

  

Look at the feeling is still very strange, a little do not believe the sense of code, and finally debug found that the initialization of class members affect the special case data

Missing (override assignment).

  


The following code studies the initialization of class members, some special and subtle places to note:



 

Execution Result:
















Conclusion:

1 Initialization of all static members of the inheritance system (late Father class, later subclass)

< Span style= "line-height:0.5cm; Font-family: ' Liberation Serif ', ' Times New Roman ', Serif ' >2 The initialization of the parent class is complete (initialization of ordinary members -- constructor call)

3 Subclass Initialization (normal member - constructor)


With the first example of a bug scenario, it is clear that when the parent class is constructed, the members of the Child class (which needs to be initialized) are initialized, which

When the assigned member List is reinitialized, then the list data loss bug appears, solving the problem as long as the assignment is placed in the subclass's own constructor

(after the member is initialized), the problem is resolved.

  

So the question is, why is the same member variable intvalue without data loss? Continue initializing parsing debug, discovering non-static members,

If you don't have an explicit initialization assignment , the data of the corresponding type has the default initial starting value ( ). In a class non-static member

initial These values are not assigned in the process, so the After assigning values in the parent class constructor, the data can be preserved. so,

allele of the Bugs, The simple solution can also directly remove the display assignment of the defined place OK up.


Something:

1.JavaClasses in a class are dynamically loaded,Statica member can be used without an instantiation of the class, and for static variables there is only one copy in memory (save), theJVM

Allocates memory only once for static, and in the process of loading the class, the memory allocation of the static variable is completed. During class initialization, the class initialization step is reduced, and the code optimization has some

High.

2. Class members operate as much as possible to invoke operations in their own classes, as much as possible to compound class design principles: Single duty. Do not generate too many complex reference relationships, when a bug is generated

To analyze the cause as soon as possible.

3. Class is a non-static member definition, if the business logic allows, try not to assign a value when defining it, so that the class can be constructed with fewer initialization steps.

Accelerates initialization of the class.








Initialize from Java class to see Code optimization

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.