A deep analysis of common code blocks, construction code blocks and static code blocks in Java _java

Source: Internet
Author: User

Order of execution: (priority is from high to low.) )

Static code block >mian method > Construction Code block > Construction method.

Where the static code block is executed only once. The construction code block executes every time the object is created.

1. Normal code block

public static void Main (string[] args) {/
* Normal code block:
* A direct definition of "{Normal Code Execution statement}" in a method or statement is called a normal code block.
* Normal code block execution order is determined by the order in which they appear in the code-"first appear first"
* * * * * * * * *
System.out.println ("Here is normal code block a");
}
New A ();
{
System.out.println ("Here is normal code block B");
}

Execution result: Here is the normal code block a
This is normal code block B.

2. Static code block and Construction code block

A block of code declared in Java using the static keyword.

Often used for initialization of classes, each static block of code executes only once (when a class is loaded in memory, the class already exists after it is loaded in memory) because the JVM executes a static code block when the class is loaded, the static code block is executed before the main method. If a class contains more than one static block of code, it is executed following the code that is defined first, followed by the defined code.

Ps:

1 static code blocks cannot exist in any method body.

2 static code blocks cannot directly access static instance variables and instance methods, which need to be accessed through instance objects of the class.

Building blocks: Code blocks that are defined directly in a class and that do not have a static keyword are called {} to construct a code block.

The construction code block is invoked when the object is created, each time the object is created, and the execution order of the Construction code block takes precedence over the class constructor.

public class Structure {
{
System.out.println ("Here is normal code block");//All classes have a default constructor, where the code block is a constructed block of code that executes
when an object in the class is created The public
static void Main (string[] args) {/
* Normal code block:
* A direct definition of "{Normal Code Execution statement}" in a method or statement is called a normal code block.
* Normal code block execution order is determined by the order in which they appear in the code-"first appear first"
* * * * * * * * *
System.out.println ("Here is normal code block a");
}
New structure ()///second class load static code block does not execute
//new A ();
{
System.out.println ("Here is normal code block B");
}
static{
System.out.println ("Here is a static code block");
}

Execution results:

Here is the static code block//precedence over the main function
This is normal code block a.
Here is the normal code block//The object in the class is executed when it is created, every time it is created, in addition to a new structure (); The results of the execution are:

This is normal code block B.

3. Summary Summary

public class Structure {
{
System.out.println ("Here is normal code block");
}
public static void Main (string[] args) {
{
System.out.println ("Here is normal code block a");
}
New structure ();
New structure ();
New A ();
{
System.out.println ("Here is normal code block B");
}
static{
System.out.println ("Here is a static code block")
;
}
Class a{
static{
System.out.println ("Here is the general static code block in a 1");
{
System.out.println ("Here is the normal code block in a 1");
}
{
System.out.println ("Here is the normal code block in a 2");
}

Execution results:

This is a static code block.
This is normal code block a.
Here is the general static code block in a 1
Here is the normal code block in a 1
Here is the normal code block in a 2
This is normal code block B.

Precedence Summary: Static code block >main () > Building code block

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.