public void onClick(View v) { switch (v.getId()) { case R.id.start: { Intent intent = new Intent(); intent.setClass(this, Menu.class); startActivity(intent); } break; case R.id.records: Intent intent = new Intent(); intent.setClass(this, RecordList.class); startActivity(intent); break; } } 

In the processing of the first button there are braces, I would like to understand how they work in this case. If you remove them, there will be a complaint that two identical names are intent , even if the break operator is worth it. If I suddenly write such a code that the first button will not go over the created intent and I will have the opportunity to click on the second button, then something bad will happen.

  • 2
    interesting implementation) advice on the merits of the question: move the Intent = new Intent() and startActivity(intent) beyond the switch . If he repeats himself there anyway - Jarvis_J
  • Well, in this case, your solution is correct, but I have buttons in the switch where the general intent is not created, for example, the exit button, all of a sudden I will click on the button where the intent is not needed. Although given that the buttons, the likelihood of clicking on the buttons where the intent will be created is higher, then you can and make. - Turalllb
  • one
    Well, I do an if (intent!=null) startActivity(intent) . But in general - the answer is normally written as it can be done) - Jarvis_J

2 answers 2

{ ... } in this case is just a (anonymous) block of code. And this was done, apparently, only to ensure that there was no conflict of variable names.


This answer states that the use of such blocks is often a sign of poorly written code. The statement, of course, can be considered controversial, but specifically in this case the creation of the Intent , as well as the calls to the setClass and startActivity can be taken out of the switch , leaving only the choice of the class in the case :

 Class clazz = null; switch (v.getId()) { case R.id.start: clazz = Menu.class; break; case R.id.records: clazz = RecordList.class; break; } if (clazz != null) { Intent intent = new Intent(); intent.setClass(this, clazz); startActivity(intent); } 
  • My commentary written by Jarvis_J is questionable; this also applies to what you say. But I was more interested in what is an anonymous block of code. With some things in java, I'm not very familiar. - Turalllb
  • one
    @Turalllb I personally did all the years of writing Java code without such blocks. This does not mean that they should not be used at all, but still. If in case different actions, then in order not to contact anonymous blocks of code, you can alternatively use: 1. if-else ; 2. The removal of the bodies of case in separate methods; 3. Carrying out processing into separate classes (<-> button handler button) and replacing switch -a to select the desired handler. What is considered a good approach from this is a subjective matter, plus depends on the specific situation. - Regent
  • Yes, I understand you, the code almost did not come across anonymous blocks and they do not look very readable. Just to know, and the inetviewers love all sorts of such useless practice tasks - Turalllb

A pair of curly braces {...} sets the boundaries of the code block . In Java, variables can be declared in any block. The block determines the scope (visibility) of the variables. In your example, the three scopes of variables (even 4, if we count the fields of the class) are nested into each other.

 public void onClick(View v) {// ΠΎΠ±Π»Π°ΡΡ‚ΡŒ 1 switch (v.getId()) {// ΠΎΠ±Π»Π°ΡΡ‚ΡŒ 2 case R.id.start:{//ΠΎΠ±Π»Π°ΡΡ‚ΡŒ 3 } } } 

In this case, variables declared in area 1 are available from area 2 and 3, variables obyavlyannye in area 2 will be available in area 3, but not available in area 1. However, the permanent from area 3 are not available beyond the limit of this area. For example, the following code will not compile until you comment out line 22, because the variable in this place is no longer available.

 public class FieldVisiblTester { //ΠΎΠ±Π»Π°ΡΡ‚ΡŒ1 - поля класса, Π²ΠΈΠ΄ΠΈΠΌΡ‹ Π²Π΅Π·Π΄Π΅ Π²Π½ΡƒΡ‚Ρ€ΠΈ класса String s1 = "string1"; public static void main(String[] args) { new FieldVisiblTester().method(); } void method(){ //ΠΎΠ±Π»Π°ΡΡ‚ΡŒ2 - Π»ΠΎΠΊΠ°Π»ΡŒΠ½Ρ‹Π΅ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Π΅ ΠΌΠ΅Ρ‚ΠΎΠ΄Π°, Π²ΠΈΠ΄ΠΈΠΌΡ‹ Π²Π½ΡƒΡ‚Ρ€ΠΈ ΠΌΠ΅Ρ‚ΠΎΠ΄Π° String s2 = "string2"; { //ΠΎΠ±Π»Π°ΡΡ‚ΡŒ3 - ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Π΅ Π±Π»ΠΎΠΊΠ° ΠΊΠΎΠ΄Π°, Π²ΠΈΠ΄ΠΈΠΌΡ‹ Π²Π½ΡƒΡ‚Ρ€ΠΈ Π±Π»ΠΎΠΊΠ° String s3 = "string3"; System.out.println("inside code block"); System.out.println(s1); System.out.println(s2); System.out.println(s3); } System.out.println("inside method"); System.out.println(s1); System.out.println(s2); //пСрСмСнная s3 нСдоступна Π²Π½Π΅ Π±Π»ΠΎΠΊΠ°, Π³Π΄Π΅ ΠΎΠ½Π° объявлСна System.out.println(s3); } } 

Mistake

FieldVisiblTester.java:22: error: cannot find symbol System.out.println (s3);

symbol: variable s3 location: class FieldVisiblTester 1 error

Another interesting point is that in Java you cannot "hide / block" variables with a block of code. The following code will not compile (error: variable s2 is already defined in method method ())

 void method(){ //ΠΎΠ±Π»Π°ΡΡ‚ΡŒ2 - Π»ΠΎΠΊΠ°Π»ΡŒΠ½Ρ‹Π΅ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Π΅ ΠΌΠ΅Ρ‚ΠΎΠ΄Π°, Π²ΠΈΠ΄ΠΈΠΌΡ‹ Π²Π½ΡƒΡ‚Ρ€ΠΈ ΠΌΠ΅Ρ‚ΠΎΠ΄Π° String s2 = "string2"; { //ΠΎΠ±Π»Π°ΡΡ‚ΡŒ3 - ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Π΅ Π±Π»ΠΎΠΊΠ° ΠΊΠΎΠ΄Π°, Π²ΠΈΠ΄ΠΈΠΌΡ‹ Π²Π½ΡƒΡ‚Ρ€ΠΈ Π±Π»ΠΎΠΊΠ° // Ρ‚.ΠΊ s2 объявлСна Π² "Π½Π°Ρ€ΡƒΠΆΠ½ΠΎΠΌ" Π±Π»ΠΎΠΊΠ΅, ΠΎΠ½Π° Π²ΠΈΠ΄ΠΈΠΌΠ° ΠΈ Ρ‚ΡƒΡ‚ // Ρ‚.Π΅ нСльзя ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ Ρ‚ΠΎ ΠΆΠ΅ имя String s2 = "string3"; } } 

But you can do it

 void method(){ { String s2 = "string3"; } //Π²Ρ‹ΡˆΠ»ΠΈ ΠΈΠ· Π±Π»ΠΎΠΊΠ° ΠΊΠΎΠ΄Π°, пСрСмСнная s2 Π½Π΅ Π²ΠΈΠ΄ΠΈΠΌΠ° //ΠΌΠΎΠΆΠ½ΠΎ ΠΎΠ±ΡŠΡΠ²ΠΈΡ‚ΡŒ Π΅Ρ‰Π΅ Ρ€Π°Π· String s2 = "string2"; } 
  • at the expense of the last piece of code: is the variable s2 invisible or just gone into oblivion and the garbage collector will soon reap it? I mean, the fields in the code block lose links after the code block is completed, right? - Turalllb
  • If we talk about this particular example, the link to the object no longer exists and the object itself can be given to the garbage collector. But you need to understand that in a block of code, for example, you can put a String object in an array of strings, then after leaving the block, the object itself cannot be destroyed because the link to it is still in the array. Even when working with String, you should consider using the String constant pool. - relativizt