Good day. Actually, the question is, what is the practical meaning of the construction, when a block inside the method stands out. Something like this:
void method() { { System.out.println("что-то делаем") } //и остальной код } Good day. Actually, the question is, what is the practical meaning of the construction, when a block inside the method stands out. Something like this:
void method() { { System.out.println("что-то делаем") } //и остальной код } There are several scenarios in which blocks can be used.
For variables defined in a block, the scope is a block. As a result, in different blocks you can define variables with the same name:
{ String test = "test"; //используем test в первом блоке } { String test = "test2"; //используем test во втором блоке } You can put a label on the block, after which you can exit the block using break .
A: { //где-то в глубине выходим из блока if(isFinishedA()) { break A; } //продолжается блок } //продолжается метод It is worth noting that in all the above cases, instead of a block, you can use other approaches (division into methods, tests, logic change). Accordingly, the need for local units is controversial.
A similar question in English: Is there a method of Java?
Source: https://ru.stackoverflow.com/questions/633142/
All Articles
System.outor something else? - default locale