[Java] Static & initialization execution sequence

Source: Internet
Author: User

About static

Static methods cannot access non-static variables. All other MS functions are supported.

Static methods do not use new objects and are called directly by class names. (For example, math. Random ())

Final variable to be initialized

As long as the main is in this class, the static block static {} is first executed (first parent class, then subclass), and then the commands in main are executed. The constructor is also a parent class and a Child class. (See headfirstp310)

{} Is a non-static block. When a constructor is encountered, it is executed before each specific Constructor (first parent class {}, then parent class constructor, then subclass {}, constructor)

<Original statement: A non-static block can be considered as the first part of the constructor after executing the parent class constructor. In short, it is a non-static block before the constructor.

Explicit static initialization (static {blahblahbalh}) is run during object access. Non-static instance initialization (directly included in {}) is run before the constructor is called, for details, see thinking in Java P 186>

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.