java-code block (learn it yourself as a note)

Source: Internet
Author: User

* A: Code block overview
* In Java, code enclosed in {} is called a code block.
* B: code block classification
* Depending on their location and declaration, can be divided into local code block, construction code block, static code block, synchronous code block (multi-threaded interpretation).
* C: Application of common code blocks
* A: Local code block
* appear in method, limit variable life cycle, release early, improve memory utilization

public static void Main (string[] args) {
{
int i = 20;
System.out.println (i); A local code block is a block of code in a method that is valid only within {}
}

}


* B: Constructs a block of code (initialization block)
* occurs outside of a method in a class, the same code is stored together in multiple construction method methods, each call construct executes, and is executed before the method is constructed

Executes the code block once for each object created;
* C: Static code block

static {

}

* occurs outside the method in the class, with the static modifier, used to initialize the class, executes at load time, and executes only once.
* Initializes the class, typically for loading the driver

Static code block is better than Main method execution

  

java-code block (learn it yourself as a note)

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.