[英]Why can't I access an enum from an instance of the class where it's defined?
In the example below, if I try to access the enum through an instance of Class1 from Class2, I get the error: "unexpected type": 在下面的示例中,如果尝试通过Class2的Class1实例访问枚举,则会收到错误:“意外类型”:
public class Class1 {
public enum EnumExample {
ONE;
}
}
public class Class2 {
public Class2 {
Class1 class1 = new Class1();
Class1.EnumExample enumEx = class1.EnumExample.ONE;
}
}
I can access it using Class1.EnumExample.ONE
which led me to believe that I can't access it using an instance because the enum is static, but I can access other static variables through the instance, so there must be more to it than that. 我可以使用
Class1.EnumExample.ONE
来访问它,这使我相信我不能使用实例来访问它,因为枚举是静态的,但是我可以通过该实例访问其他静态变量,因此它必须比那。
So why does this happen? 那么为什么会这样呢?
Also, I know this probably should be another question, but I'm including this here as an aside in case it's an easy answer: Is there some way for me to access the enum from an instance and still maintain the convenience of the enum's auto-completion in an IDE (Netbeans in this case). 另外,我知道这可能应该是另一个问题,但是我将其作为一个简单的答案放在了一边:我是否可以通过某种方式从实例访问枚举,并且仍然保持枚举自动的便利性IDE中的-completion(在这种情况下为Netbeans)。
I tried to return the enum in a static method from Class1 in hopes of exploiting the fact that I can access static methods from its instances: 我试图从Class1的静态方法中返回枚举,希望利用我可以从其实例访问静态方法的事实:
public static Class<EnumExample> getEnumExample() {
return EnumExample.class;
}
But when I access it on Class2, Netbeans fills the options with methods from Class
, and not specifically from EnumExample
. 但是,当我在Class2上访问它时,Netbeans会使用
Class
方法而不是EnumExample
方法来填充选项。
I could just use Class1.EnumExample
, but I'm trying to avoid using the class' name in here for convenience and type-safety, because this code will be reused in different places. 我可以只使用
Class1.EnumExample
,但是为了方便起见和类型安全,我试图避免在这里使用类的名称,因为此代码将在不同的地方重用。
From the specification 从规格
A nested
enum
type is implicitly static.嵌套
enum
类型是隐式静态的。 It is permitted for the declaration of a nested enum type to redundantly specify the static modifier.允许嵌套枚举类型的声明多余地指定static修饰符。
Regardless, a type is not a member of an instance. 无论如何,类型不是实例的成员。 It is (possibly) a member of a type.
它是(可能是)类型的成员。
As the specification says, a field access expression takes the following form 如规范所述, 字段访问表达式采用以下形式
FieldAccess :
FieldAccess :
We care about the first case, with a Primary
expression. 我们关心第一种情况,带有
Primary
表达式。 You have to access the enum
constant through its type name, through the name of its enclosing type. 您必须通过其类型名称,通过其封闭类型的名称来访问
enum
常量。
You say 你说
I could just use Class1.EnumExample, but I'm trying to avoid using the class' name in here for convenience and type-safety,
我可以只使用Class1.EnumExample,但是为了方便起见和类型安全,我试图避免在这里使用类的名称,
There's not much more type safe than using the type name in source code. 没有比在源代码中使用类型名称更安全的类型。
A Class
instance and a type name are two completely different things. Class
实例和类型名称是完全不同的两件事。 The type java.lang.Class
is a type like any other ( String
, List
, ExecutorService
, etc.). 类型
java.lang.Class
与其他类型( String
, List
, ExecutorService
等)一样。 It has its fields and methods. 它具有其领域和方法。 If you have a value of type
Class
, you can use it to access those members. 如果您具有
Class
类型的值,则可以使用它来访问那些成员。 With a type name, you can declare variables, you can declare type arguments, create new instances (with new
), and invoke static
methods. 使用类型名称,您可以声明变量,可以声明类型参数,创建新实例(使用
new
),并调用static
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.