About common code blocks in Java, building blocks of code, and static blocks of code

Source: Internet
Author: User

1. Common code Blocks

public static void Main (string[] args) {
/* Common code block:
* 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 Occurrence first"
* */
{
System.out.println ("This is normal code block a");
}
New A ();
{
System.out.println ("This 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 code block executes only once (classes are executed when loaded in memory, classes already exist after loading in memory) because the JVM executes static blocks of code as it loads classes, so static blocks of code are executed before the main method. If a class contains more than one static block of code, it is executed after the code that is defined first, followed by the definition.

Ps:1 static code blocks cannot exist in any method body. 2 Static code blocks do not have direct access to static instance variables and instance methods and need to be accessed through instance objects of the class.
Building blocks: Blocks of code that are defined directly in the class and do not have the static keyword are called {} construction blocks of code. Construction blocks are called when the object is created, each time the object is created, and the order in which the code block is constructed takes precedence over the class constructor.

public class Structure {
{
System.out.println ("This is the normal code block");//There is a default constructor in all classes, where the code block is the construction code block, which executes when an object in the class is created
}
public static void Main (string[] args) {

/* Common code block:
* 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 Occurrence first"
* */
{
System.out.println ("This is normal code block a");
}
New structure ();//The Static code block does not execute when the class is loaded for the second time
New A ();
{
System.out.println ("This is normal code block B");
}
}

static{
System.out.println ("Here is the static code block");
}
}

Execution Result:

Here is the static code block//precedence over the main function
This is the normal code block a.
Here is the ordinary code block//class object is created when executed, each time the creation is executed once, in addition to a new structure (); The result of the execution is:

This is normal code block B.

3. Summary

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

}

Execution Result:

Here is the static code block
This is the normal code block a.
Here is the 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.

Priority summary: Static code block >main () > Construct code block

About common code blocks in Java, building blocks of code, and static blocks of code

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.