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("что-то делаем") } //и остальной код } 
  • four
    @ Anton Mukhin If a programmer’s work is estimated in an organization by the number of characters typed, then it has a direct practical meaning. - Vlad from Moscow
  • Is there System.out or something else? - default locale
  • @defaultlocale Anything. System.out I wrote as an example of some kind. - Anton Mukhin

1 answer 1

There are several scenarios in which blocks can be used.

  1. Limiting the scope of variables.

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 во втором блоке } 
  1. Named labels.

You can put a label on the block, after which you can exit the block using break .

 A: { //где-то в глубине выходим из блока if(isFinishedA()) { break A; } //продолжается блок } //продолжается метод 
  1. Debugging On the line inside the block, you can put a breakpoint.

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?