About Question enthuware.ocpjp.i.v11.2.3231 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
sir_Anduin@yahoo.de
Posts: 62
Joined: Fri Aug 07, 2015 2:16 pm
Contact:

About Question enthuware.ocpjp.i.v11.2.3231 :

Post 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

Code: Select all

 var b 
ever be decared?
the scopes of a switch case are messed up, I guess.

FlatPanda
Posts: 8
Joined: Sun Feb 23, 2020 6:51 am
Contact:

Re: About Question enthuware.ocpjp.i.v11.2.3231 :

Post 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)."

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.i.v11.2.3231 :

Post 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!
If you like our products and services, please help us by posting your review here.

FlatPanda
Posts: 8
Joined: Sun Feb 23, 2020 6:51 am
Contact:

Re: About Question enthuware.ocpjp.i.v11.2.3231 :

Post 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);
}

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.i.v11.2.3231 :

Post by admin »

Horses held. I should have double checked before assuming anything :thumbup:
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 21 guests