English 中文(简体)
为什么在某个案件贴上标签之后宣布的物体在另一些情况下是存在的? [复制]
原标题:Why object declared after one case label is available in others? [duplicate]
  • 时间:2011-05-18 08:20:23
  •  标签:
  • java
This question already has answers here:
Closed 12 years ago.

Possible Duplicate:
Variable scope in a switch case

我这样说:

switch(a) {
case b:
 Object o = new Object();
 return o;
case c:
 o = new Object();
 return o;
 }

并且令人感兴趣的是,即使没有达到第一个国家,它为什么会使用第一种病例标签后宣布的变量?

最佳回答

尽管存在不同的情况,但<代码>switch<>/code>的当地变量表也属于同一组,这意味着这些变量的范围相同。

我知道, Java的新范围只是在新的法典体内创造的。 一组法典(有一条以上线)必须用曲折环绕。 换文的代码为not,环绕于曲折,因此是整个发言范围的一部分。

然而,你实际上可以通过增加粗略的镜子,在发言中引入一个新的范围:

switch (cond) {
case 1:{
     Object o = new Object();
}
    break;
case 2:{
    // Object o is not defined here!
}
    break;
}
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签