Execution sequence of static attributes, static code blocks, static methods, constructor methods, and common methods

Source: Internet
Author: User

Static code block:

When a class is loaded (memory), (called first), the static code block is executed and executed once. Static blocks are often used to initialize class attributes. The execution class is called before it is loaded.

 

Non-static code block:

When a class object is created and loaded (memory), (first called), each created object, that is, each loaded object, and non-static code block, is executed once. It is called before the execution class object is loaded.

 

Before an object is loaded, the class must be loaded first.

 

Static and non-static methods: they are executed only when called. Static Methods belong to Classes. After loading a class, you can call static methods. Non-static methods belong to objects. After loading an object, you can call non-static methods.

 

The action of loading a class is not symbolic, but the action of loading an object is a new action and will be executed.

 

Simplified:

Static code block: the class is called once every time it is loaded into the memory and called first.

Non-static code block: class objects are called once every time they are loaded into the memory, and then the corresponding constructor is called first.

Static Method and non-static method: both are executed when the method is called, and the static method is called, provided that the class has been loaded into the memory; non-static method is called, the premise is that the class object has been loaded into the memory.

 

So,

When the class is loaded/When the class is loaded: [when the class is used, it will be loaded]
First, when the class object is generated, the class and all its parent classes will be loaded;
2. When accessing static members of the class;
Third: class. forname ("Class Name ");

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.