sdasdadas
sdasdadas

Reputation: 25136

Why isn't the scope of a switch statement in Java limited?

Why, in Java, is a variable's scope confined to a switch block as opposed to a case block. For example,

// Scope limited to a switch block
switch (number) {
case 1:
    String result = "...";
    break;
case 2:
    result = "...";
    break;

In the above example, result needs only to be declared once. If you declare it twice then you receive a Duplicate local variable message.

My question is: how does the program know you've declared result if number = 2? (It won't fall into case 1 and won't declare the variable... or will it?)

EDIT:

I might be confusing everyone. I understand how I can limit the scope of a variable but my question is: how does Java know that result has been declared if it doesn't fall into the case?

Upvotes: 6

Views: 2446

Answers (7)

zw324
zw324

Reputation: 27220

EDIT: Java uses lexical scoping (also called static scoping), so the scope of the variables are determined during compile time, and have nothing to do with the actual evaluation.

Java is block scoped, so it's scope will respect the {} in the example above.

See JLS 6.3:

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.

Upvotes: 7

user207421
user207421

Reputation: 310980

Because there isn't a 'case block'.

Everything inside the switch statement is in the same block. You can't declare the same variable twice in the same block.

If you want a 'case block' you have to write the { and } yourself.

Upvotes: 0

Bruce Martin
Bruce Martin

Reputation: 10543

The reason a variable scope is confined to a switch block as opposed to a case block is java allows for fall through from one case block to the next i.e.

switch (number) {
case 1:
     .... some code ...
case 2:
     .... some more code ...

in this case case if number is 1, both case 1 & case 2 are executed. The break acts like a goto end of select, it does not ever end scope. The variable scope has to be at select level.

As others have stated, use a block to restrict scope i.e.

switch (number) {
case 1:{
    String result = "...";
    break;
}
case 2:{
    String result = "...";
    break;
}

Upvotes: 0

ZhongYu
ZhongYu

Reputation: 19682

In the grandfather language Fortran, there is a computed GOTO statement

        GOTO expr
        ...
    1   ...
        ...
    2   ...
        ...

based on the value of expr, the code jumps to 1, 2 etc.

C's (and Java's) switch statement is basically a computed GOTO in disguise. We have a continuous piece of code with some labels, and we jump to one of the label. If there's no break we'll execute the rest of the block.

This rather low level control mechanism is contrary to the intuitions of today's programmers; we would think that the switch statement selects one clause and executes that clause, much like an if-elseif-elseif-...-else statement.

Java inherited C's switch semantics since they didn't want to deviate from C too much. Newer languages are unlikely to continue the mistake.

Upvotes: 3

Tomer Arazy
Tomer Arazy

Reputation: 1823

What do you mean how? In order to understand the answer you need to learn how compilers work. Think of the switch as a large block with multiple "goto" statements at the end of each switch statement.

I'm not sure how java unroll switch statements but one easy and simple way to do it is this (pseudo byte-code):

    if (number == 1) goto label1;
    if (number == 2) goto label2;
    goto labelEnd;
label1:
    String result = "...";
    goto labelEnd;
label2:
    result = "...";
    goto labelEnd;
labelEnd:
    <some code>

Upvotes: 2

Csaba Szugyiczki
Csaba Szugyiczki

Reputation: 456

You can limit the scope to case blocks by adding curly brackets like this:

// Scope limited to a switch block
switch (number) {
case 1:{
    String result = "...";
    break;
}
case 2:{
    String result = "...";
    break;
}

Upvotes: 6

Jason Sperske
Jason Sperske

Reputation: 30436

Java uses block scoping, select cases aren't blocks (they are more like labels). This would work though:

switch (key) {
case 1: {
    String result = "1";
    return result ;
  }
case 2: {
    String result = "2";
    return result ;
  }
}

Upvotes: 2

Related Questions