The legal use of JAVA basic statement labels, and what is the use of {} statement blocks?

Source: Internet
Author: User

Write such a piece of code: Copy codeThe Code is as follows: int I;
{
Int j = 1;
I = j;
}

If this code exists in the class definition area, we know that it is a common statement block used to initialize class attributes. It will be called during class instantiation, you can execute some methods.
In many instances, it is used in the singleton and other modes. Previously, a static file is added to initialize complex class content, which can avoid runtime exceptions caused by loading sequence.
But what if this code appears in the method?
It has no meaning at all. In my previous thought, it was a format that included the code.
Today I wrote a bit of Code related to the "Statement tag:Copy codeThe Code is as follows: label17: int I;
Int j;
ThreadGroup [] arrayOfThreadGroup;
If (flag)
Break label17;
Return 0;

"Syntax error on token": ", {expected after this token" exception occurred at the ":" location ".
That is to say, when the Code cannot exist in a single line (int I must have a clear instantiation/assignment position inside the method body), label17 needs to be marked with statement blocks.
The correct format is:Copy codeThe Code is as follows: label17 :{
Int I;
Int j;
ThreadGroup [] arrayOfThreadGroup;
If (flag)
Break label17;
Return 0;
}
Or:
Label17:
Int I;
Int j;
ThreadGroup [] arrayOfThreadGroup;
If (flag ){
Break label17;
Return 0 ;}

Let's take a look at the incorrect usage:Copy codeThe Code is as follows: label13: int x = 0;

Obviously, there is a default single-line statement block after the tag. This x cannot be used at any position in the future. It is incorrect. The prompt is as follows:
Multiple markers at this line
-X cannot be resolved to a variable
-Syntax error on token "int", delete this token
There are two correct formats:Copy codeThe Code is as follows: int x = 0;
Label13: x = 0;
Or
Label13: {int x = 0 ;}

Therefore, we assume that the logic if () is () and statement block {} should be independent of each other.

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.