为什么在一个 case 块中定义的变量可以在其他 case 块中使用?

发布于 2024-12-24 16:23:41 字数 1184 浏览 3 评论 0原文

可能的重复:
为什么 case 语句中的变量不是本地变量?

定义的变量作用域块中的内容不能在其外部使用。例如,以下代码片段无效:

{
    int anothervar = 4;
}
{
    System.out.println(anothervar);
}

但看起来 case 块不会创建单独的作用域。

switch (mode) {
    case ONE:
        dosomething();
        return;
    case TWO:
        int[] someints = new int[] { 2, 3, 5, 7 };
        SomeObject obj = new SomeObject();
        return;
    case THREE:
        someints = new int[] { 1, 4, 6, 8, 9 };
        obj = new SomeObject();
        return;
}

为什么我不必在 case THREE 'block' 内声明 someints
假设 mode = THREE,则永远不会达到变量 someints 的声明,因为 case TWO,其中 someints 是已声明,则被跳过。或者不是吗? 它内部是如何工作的?

为什么 case 语句中的变量不是局部变量? 指出 switch 语句内部是一组 jump 命令,但这仍然无法解释变量 someints 在哪里code> 已声明。)

Possible Duplicate:
Why are variables not local in case statements?

A variable defined in a scope block can't be used outside it. For example, the following code snippet is invalid:

{
    int anothervar = 4;
}
{
    System.out.println(anothervar);
}

But it looks like a case block does not create apart scopes.

switch (mode) {
    case ONE:
        dosomething();
        return;
    case TWO:
        int[] someints = new int[] { 2, 3, 5, 7 };
        SomeObject obj = new SomeObject();
        return;
    case THREE:
        someints = new int[] { 1, 4, 6, 8, 9 };
        obj = new SomeObject();
        return;
}

Why don't I have to declare someints inside the case THREE 'block'?
Suppose mode = THREE, then the declaration of variable someints is never reached, because case TWO, where someints is declared, is skipped. Or isn't it? How does it work internally?

(The chosen answer in Why are variables not local in case statements? states that a switch statement is internally a set of jump commands, but still that does not explain where the variable someints is declared.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

埖埖迣鎅 2024-12-31 16:23:41

局部变量的作用域位于块内,如 名称文档

块中局部变量声明的范围(第 14.4.2 节)是
声明出现的块的其余部分,从其开始
自己的初始化程序(§14.4)并包括任何进一步的声明符
就在局部变量声明语句中。

块被定义为具有封闭的大括号,正如 块和语句文档

块是一系列语句、局部类声明和
大括号内的局部变量声明语句。

a local variable's scope is inside a block, as specified in the Names documentation:

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

A block is defined to have enclosing braces, as written as well in the Blocks and Statements documentation:

A block is a sequence of statements, local class declarations and
local variable declaration statements within braces.

娇纵 2024-12-31 16:23:41

这就是为什么您有时会看到带有 : 的代码:

case 0: {
   // Stuff
}

只有 { } 分隔块。

This is why you sometimes see code with :

case 0: {
   // Stuff
}

Only the { } delimitates blocks.

九厘米的零° 2024-12-31 16:23:41

声明的范围由 { ... } 标记,您可能会在一秒钟内从一种情况中掉下来。虽然我承认这是一个坑。我相信在旧版本的 Java 中不允许在 case 级别声明,并且必须添加额外的大括号:

case 1: { int x; ... }

A scope of a declaration is marked by { ... } and you may fall from one case in a second. I admit it is a hole though. I believe in an older version of Java one was not allowed to declare on the case level and had to add extra braces:

case 1: { int x; ... }
暮色兮凉城 2024-12-31 16:23:41

除了其他回答者(正如他们所注意到的,{}之间的范围只有一个)之外,我还想注意到,这种做法:在一种情况下声明变量并在另一种情况下使用它,是肮脏的< /强>。稍后您可能希望删除或更改某些案例。如果是带有声明的,则必须更改另一个案例。这可能会导致错误。

彻底思考 - 为什么你的变量 someints 应该在这两种情况下?它真的是相同的东西还是两个不同的东西?

如果是一样的话:
最好在默认情况下声明公共变量并将其放在第一个。

switch (mode) {
    default:
        int[] someints;
        return;
    case ONE:
        dosomething();
        return;
    case TWO:
        someints = new int[] { 2, 3, 5, 7 };
        SomeObject obj = new SomeObject();
        return;
    case THREE:
        someints = new int[] { 1, 4, 6, 8, 9 };
        obj = new SomeObject();
        return;
}

或者将它们声明在 switch 块之外(这更糟糕)。或者将其放在定义常量的地方。

如果它们确实是不同的变量(可能只是在某些方面相似):
在每种情况下定义不同的变量。

顺便说一句:这是一个非常好的问题!

In addition to other answerers (as they noticed, the scope between {} is only one here) I would like to notice, that such practice: declaring of a variable in one case and using it in the other, is dirty. Later you can wish to remove or change some case. And if it will be the one with the declaration, you'll have to change another case. This could cause errors.

Think thoroughly - why your variable someints should be in both cases? Is it REALLY the same thing or two different?

If it is the same:
Better declare the common variables in the default case and put it as the first one.

switch (mode) {
    default:
        int[] someints;
        return;
    case ONE:
        dosomething();
        return;
    case TWO:
        someints = new int[] { 2, 3, 5, 7 };
        SomeObject obj = new SomeObject();
        return;
    case THREE:
        someints = new int[] { 1, 4, 6, 8, 9 };
        obj = new SomeObject();
        return;
}

Or declare them out of the switch block (it's worse). Or put it somewhere where you define constants.

If they really are different variables (may be only similar in some way):
define in each case a different variable.

BTW: It's a really nice question!

独夜无伴 2024-12-31 16:23:41

{ } 内的所有内容都在同一范围内。例如,如果您在情况 2 中取出 return;,则可以在情况 3 中使用该变量,前提是该变量已初始化。

Everything inside the { } is in the same scope. If you take out the return; in case TWO for example, you can use the variable in case THREE, provided it's initialised.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文