The legitimate use of the Java underlying statement label, and what is the use of {} statement blocks? _java

Source: Internet
Author: User
Tags class definition
Like writing a piece of code like this:
Copy Code code as follows:

int i;
{
int j=1;
I=j;
}

If this code exists in the class definition area, then we know that it is a normal statement block that initializes the contents of the class attribute, which is invoked when the class is instantiated, and it can execute some methods.
In many instances, it is used in singleton patterns, preceded by a static, to initialize content for complex classes, and to avoid some run-time exceptions due to the load sequence.
But what if the code appears in the method?
It has no meaning at all. In my own previous thoughts, it was a form of code, no other.
Today I wrote a bit of code related to the statement label:
Copy Code code as follows:

Label17:int i;
Int J;
Threadgroup[] Arrayofthreadgroup;
if (flag)
Break label17;
return 0;

":" The Exception "Syntax error on Token": ", {expected to this token."
In other words, when the code cannot exist in a single line (int I must have an explicit instantiation \ Assignment position within the method body), Label17 needs to be marked with a statement block.
The correct format is:
Copy Code code 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 look at the wrong usage:
Copy Code code as follows:

Label13:int x = 0;

Obviously, there is a default one-line statement block after the label, and this x cannot be used anywhere in the future. The prompts are as follows:
Multiple markers at-line
-X cannot be resolved to a variable
-Syntax error on token ' int ', delete this token
There are two types of correct formats:
Copy Code code as follows:

int x = 0;
label13:x = 0;
Or
label13:{int x = 0;}

So it is inferred that a previous misunderstanding, for () {},if () {} usage, the logical if () and statement block {} should be two separate syntaxes.
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.