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"; }
Intent = new Intent()andstartActivity(intent)beyond theswitch. If he repeats himself there anyway - Jarvis_Jswitchwhere 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. - Turalllbif (intent!=null) startActivity(intent). But in general - the answer is normally written as it can be done) - Jarvis_J