Java code block

Source: Internet
Author: User

Http://www.cnblogs.com/end/archive/2012/12/21/2827554.html

There are four blocks of code:

Normal code block: Inside the method, there is a block of code enclosed in curly braces.

For example:

public void Test () {System.out.println ("hij");
This is the code block {
int x; System.out.println ("Hello World," +x);} System.out.println ("Hello");
This is also the code block {return;}}

When you put the return code block to the front, that is, the return code block after the code, it will be an error.

When we have finished using X and no longer use X in the next code, there is no need for x to take up space in memory, which, in the case of a local code block, encapsulates the X and the area it is designed to be a partial block of code that is not changed in the execution of the program, but disappears after execution is complete.

Building blocks of code: blocks of code defined directly in the class, without the static keyword, which can initialize the object of the class

When a constructor in a class exists in an overloaded form, and there is a common member variable or a common method, it can be initialized by constructing a code block, which reduces the duplication of code!

Static code block: a code block decorated with static

Executes as the class loads, and executes only once.

takes precedence over the main function execution. Class can be initialized to

When the methods in the class are statically, and the constructor is private, it is not possible for us to instantiate this class, but to let the class add some properties, we can use the static code block

Java code block

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.