Java 为什么不能在方法中本地声明枚举?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/700831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why can't enums be declared locally in a method?
提问by bruno conde
Today, I found myself coding something like this ...
今天,我发现自己在编写这样的代码......
public class LocalEnums {
public LocalEnums() {
}
public void foo() {
enum LocalEnum {
A,B,C
};
// ....
// class LocalClass { }
}
}
and I was kind of surprised when the compiler reported an error on the local enum
:
当编译器在本地报告错误时,我有点惊讶enum
:
The member enum LocalEnum cannot be local
成员枚举 LocalEnum 不能是本地的
Why can't enumsbe declared local like classes?
为什么不能像类一样将枚举声明为本地?
I found this very useful in certain situations. In the case I was working, the rest of the code didn't need to know anything about the enum
.
我发现这在某些情况下非常有用。在我工作的情况下,其余的代码不需要知道关于enum
.
Is there any structural/designconflict that explains why this is not possible or could this be a futurefeature of Java?
是否有任何结构/设计冲突可以解释为什么这是不可能的,或者这可能是Java的未来特性?
回答by kdgregory
Enums are static nested classesbecause they define static member variables (the enum values), and this is disallowed for inner classes: http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1.3
枚举是静态嵌套类,因为它们定义了静态成员变量(枚举值),内部类不允许这样做:http: //docs.oracle.com/javase/specs/jls/se7/html/jls-8.html #jls-8.1.3
Update:I was looking through the JLS
(java language specification) for more detail on the restrictions of static nested classes, and didn't find it (although it's probably there, hidden under a different topic). From a pure implementation perspective, there's no reason that this couldn't be done. So I suspect that it was a language philosophy issue: it shouldn't be done, therefore won't be supported. But I wasn't there, so that's pure speculation.
更新:我正在查看JLS
(java 语言规范)以获取有关静态嵌套类限制的更多详细信息,但没有找到它(尽管它可能在那里,隐藏在不同的主题下)。从纯粹的实现角度来看,没有理由不能做到这一点。所以我怀疑这是一个语言哲学问题:不应该这样做,因此不会得到支持。但我不在那里,所以这纯粹是猜测。
As a comment:if your methods are large enough that they require their own enums, then it's a strong sign that you need refactoring.
作为评论:如果您的方法足够大以至于它们需要自己的枚举,那么这是您需要重构的强烈信号。
回答by Jon Skeet
I rarely find myself writing any typeswithin a method, unless it's an anonymous inner class. You can, however, write nested enums:
我很少发现自己在方法中编写任何类型,除非它是匿名内部类。但是,您可以编写嵌套枚举:
public class NestedEnum
{
private enum MyEnum
{
X, Y, Z
}
public void foo()
{
}
}
I don't think I'd really want to reada method which declared a new type within it - do you have any concrete reason for wanting to declare it inside the method instead of just as a nested type? I can see the "no other methods need to know" argument, but I think a comment can sort that out and still leave more readable code.
我不认为我真的想阅读在其中声明了新类型的方法 - 您有什么具体的理由想要在方法中声明它而不是仅仅作为嵌套类型?我可以看到“没有其他方法需要知道”的论点,但我认为评论可以解决这个问题,并且仍然留下更具可读性的代码。
回答by Peter
http://mindprod.com/jgloss/enum.htmlgives a good description of java enums - as previously mentioned, enums are defined as static so they can't be declared as locals
http://mindprod.com/jgloss/enum.html很好地描述了 java 枚举 - 如前所述,枚举被定义为静态,因此它们不能被声明为本地人
回答by no_ripcord
It's weird because the java inner class definition says that compile-time constants can be declared static, and a member of a Enum is clearly compile-time constant, plus enum is a static class, suposedly...
这很奇怪,因为java内部类定义说编译时常量可以声明为静态,并且枚举的成员显然是编译时常量,加上枚举是静态类,据说......
文件:
8.1.3 Inner Classes and Enclosing Instances
8.1.3 内部类和封闭实例
(...) Inner classes may not declare static members, unless they are compile-time constant fields.
(...) 内部类不能声明静态成员,除非它们是编译时常量字段。
class Outer{
class Inner extends HasStatic{
static final int x = 3; // ok - compile-time constant
static int y = 4; // compile-time error, an inner class
}
static class NestedButNotInner{
static int z = 5; // ok, not an inner class
}
interface NeverInner{} // interfaces are never inner
}
回答by emory
"Nested enum types are implicitly static." 8.9 Enums
It is reasonable to infer that nested enum types implicitly contain the static access modifier.
- "It is a compile-time error if a local class declaration contains any one of the following access modifiers: public, protected, private, or static."14.3 14.3 Local Class Declarations
“嵌套的枚举类型是隐式静态的。” 8.9 枚举
推断嵌套枚举类型隐式包含静态访问修饰符是合理的。
- “如果本地类声明包含以下任何一种访问修饰符,则会出现编译时错误:public、protected、private 或 static。” 14.3 14.3 局部类声明