-2

I had this code showing up in my java course today and not even the professor could answer why this is working. I hope that one of you knows the answer and can give it to me.

With the commented part it is strangely working. If I comment the part of z=3 then it is not working. Can someone explain :D ? Thanks in advance!

public static void main(String[] args) {

        int a = 2;
        
        switch(a) {
        case 1:
            int z=6;
        case 2:
            //z=3;
            System.out.println(z);
        }
  • 2
    What exactly does "working" mean? What does this code do? – Andy Turner Apr 30 '21 at 18:17
  • https://ideone.com/UAa3ol your code doesn't compile, for the reason I'd expect: z is not definitely assigned. https://ideone.com/sS8gRR it compiles with the comment uncommented. – Andy Turner Apr 30 '21 at 18:18
  • `z` is declared in `case 1`. It hasn't been declared until then. – Zack Macomber Apr 30 '21 at 18:18
  • @HenryTwist How is a value assigned when `a` is `2`? There will be no value assigned to the variable `z`, hence the error message. – Progman Apr 30 '21 at 18:26
  • What's strange is the fact that you can uncomment `//z=3` and this works but otherwise won't. So that means `z` is available outside of the `case 1:` scope. – Zack Macomber Apr 30 '21 at 18:26

2 Answers2

2
        int a = 2;
        
        switch(a) {
        case 1:
            int z=6;
        case 2:
            //z=3;
            System.out.println(z);
        }

This code won't compile because z hasn't been definitely assigned. In the case that a == 2, no value has been assigned to it. Because it is a local variable, it must be definitely assigned before use.

If you uncomment the //z=3 line, it works because then z is definitely assigned. z is in scope there, despite having been declared in a different case, simply because that's how Java's scoping rules work in a switch case.

Specifically:

The scope of a local variable declaration in a block (§14.4) is the rest of the block in which the declaration appears, starting with its own initializer and including any further declarators to the right in the local variable declaration statement.

The block is the { ... } immediately following the switch (a). Hence, you can refer to the variable z anywhere in that block after the int z =.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

A variable declared inside a switch case is part of the local scope. You should delimit the scope.

  public static void main(String[] args) {

    int a = 2;
    
    switch(a) {
    case 1: {
        int z=6;
     // if a was 1, case 2 would be reached too, because there is no break here
    }
    case 2:{
        int z=3;
        System.out.println(z);    
    } 
    }

If you want to use z variable, you should declare it ouotside the swtich.

The commom way of doing it would be:

  public static void main(String[] args) {

    int a = 2;
    
    switch(a) {
    case 1: {
        int z=6;
     
    }
    break;
    case 2:{
        int z=3;
        System.out.println(z);    
    } 
    break;
    }
Cícero Moura
  • 2,027
  • 1
  • 24
  • 36