Java constructs static code blocks for common code blocks and static code blocks.

Source: Internet
Author: User

Java constructs static code blocks for common code blocks and static code blocks.

1. Common Code Block

Public static void main (String [] args ){
/* Common Code block:
* A code block is directly defined as a normal code block when "{execution Statement of Common Code}" appears in a method or statement.
* The execution sequence of common code blocks is determined by the order in which they appear-"First show first execute"
**/
{
System. out. println ("here is the normal code block ");
}
// New ();
{
System. out. println ("here is the normal code block B ");
}
}

Execution result: this is A common code block.
Here is the normal code block B

2. Static code block and constructed code block

Code block declared using the static keyword in java. It is often used for class initialization. Each static code block is executed only once (the class is loaded in the memory, and the class already exists after being loaded in the memory) since the JVM executes static code blocks during class loading, the static code blocks are executed before the main method. If the class contains multiple static code blocks, the Code defined first is executed according to the "first defined code, then the code defined later is executed.

Ps: 1 static code block cannot exist in any method body. 2 static code blocks cannot directly access static instance variables and instance methods. They must be accessed through instance objects of the class.
Constructor block: A code block that is defined directly in a class without the static keyword is called a {} constructor block. The constructor code block is called when an object is created. Each time an object is created, it is called. The execution order of the constructor code block is higher than that of the class constructor.

Public class structure {
{
System. out. println ("this is a normal code block"); // all classes have a default constructor. The code block here is a constructor block, which is executed when an object in the class is created.
}
Public static void main (String [] args ){

/* Common Code block:
* A code block is directly defined as a normal code block when "{execution Statement of Common Code}" appears in a method or statement.
* The execution sequence of common code blocks is determined by the order in which they appear-"First show first execute"
**/
{
System. out. println ("here is the normal code block ");
}
New structure (); // The static code block is not executed when the second class is loaded.
// New ();
{
System. out. println ("here is the normal code block B ");
}
}

Static {
System. out. println ("static code block ");
}
}

Execution result:

Here is the static code block // takes precedence over the main function
Here is the common code block
In this example, the object in the common code block // class is executed when it is created. Every time it is created, a new structure () is added. The execution result is:

Here is the normal code block B

3. Summary

Public class structure {
{
System. out. println ("this is a normal code block ");
}
Public static void main (String [] args ){
{
System. out. println ("here is the normal code block ");
}
// New structure ();
// New structure ();
New ();
{
System. out. println ("here is the normal code block B ");
}
}
Static {
System. out. println ("static code block ");
}
}
Class {
Static {
System. out. println ("here is the general static code block 1 in ");
}
{
System. out. println ("here is the normal code block 1 in ");
}
{
System. out. println ("here is the normal code block in A 2 ");
}

}

Execution result:

Static code block
Here is the common code block
Here is the general static code block 1 in.
Here is the normal code block 1 in
Here is the normal code block 2 in
Here is the normal code block B

Summary of priorities: static code blocks> Main ()> build code blocks

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.