Java static code block, construction code block, and local code block

Source: Internet
Author: User

Java static code block, construction code block, and local code block

There are three types of code blocks to be learned, which have never been known before. Now we have sorted it out. As a note, I also hope to provide some help to the viewer.

Static code block

Class Demo

{

Static // static code block

{

}

}

It is executed as the class is loaded. And only run once (this is related to the long life cycle of the class ).

Purpose: Initialize the class.

Construct code blocks

Class Demo

{

{// Construct a code block

}

}

Independent code block in the class.

Purpose: Initialize all objects.

(The constructor initializes the corresponding object .)

Partial code block

Class Demo

{

Public void demo ()

{

{// Local code block

}

}

}

Empty code block in the method.

Purpose: restrict the lifecycle of a local variable.

If you have good suggestions, you can leave a message or send to the author's mailbox: fzb_xxzy@163.com

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.