Page 1 of 1
					
				About Question enthuware.ocpjp.i.v11.2.3231 :
				Posted: Wed Sep 18, 2019 10:24 am
				by sir_Anduin@yahoo.de
				lets asume this program 
Code: Select all
public static void main(String[] args) {
        switch (1) {
        case 0:
            var b = false;
            break;
        case 1:
            b = true;
            break;
        }
    }
how can
ever be decared?
the scopes of a switch case are messed up, I guess.
 
			
					
				Re: About Question enthuware.ocpjp.i.v11.2.3231 :
				Posted: Sat Feb 29, 2020 12:32 pm
				by FlatPanda
				This is a very interesting conundrum, because in a "default" branch, the variable b is not in scope anymore. I haven't found an explicit resolution for this in the JLS, but if I reaaaaaly want, then I can interpret section 16.9.2 (Definite Assignment and Statements / switch Statements) in a way that it fits. I would have expected to find it explicitly in 6.3-2 (Scope of Local Variable Declarations) or in 14.11 (Blocks and Statements / The switch Statement).
The real mindboggling things start here:
Code: Select all
public static void main(String args[]) {
      switch(Integer.parseInt(args[0])) {
          case 0: var b = false; System.out.println(b); 
          case 1: b = true; System.out.println(b);
          default : System.out.println(args);
          case 2: b = false; System.out.println(b);
      } 
    }
Running with 0 it prints: false, true, args@hash, false (with 1: true, args@hash, false) BUT b is not defined in the default branch! i.e., s.o.p(b); is a compile time error in the line of default
@admin: this also means that the explanation text for "Compile time error at line //3", namely "There is no problem here. b is in scope for the rest of the switch block" is semi-correct: it is correct for the exact example given in the question, but 100% correct it would be "There is no problem here. b is in scope for every case-labeled block for the rest of the switch block (not for default though)."
 
			
					
				Re: About Question enthuware.ocpjp.i.v11.2.3231 :
				Posted: Sat Feb 29, 2020 11:54 pm
				by admin
				You are right. b is not in score in the default block. I couldn't find anything about it in the JLS either.
Explanation has been updated. thank you for your feedback!
			 
			
					
				Re: About Question enthuware.ocpjp.i.v11.2.3231 :
				Posted: Sun Mar 01, 2020 3:11 am
				by FlatPanda
				Stop the presses, the first analysis was wrong!
It *is* in scope, but I haven't read the exact error message *facepalm* It says (rightly!) that b might not have been intialized. This is not special to the default case, i.e., this won't work either:
Code: Select all
        switch (Integer.parseInt(args[0])) {
            case 0 : var b = true; System.out.println(b);
            case 1 :               System.out.println(b);
}
 
			
					
				Re: About Question enthuware.ocpjp.i.v11.2.3231 :
				Posted: Sun Mar 01, 2020 3:34 am
				by admin
				Horses held. I should have double checked before assuming anything  
