简体   繁体   English

为什么不能从定义了该类的实例的实例访问枚举?

[英]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

  • Primary . Identifier Primary . Identifier . Identifier
  • super . Identifier
  • TypeName . super . Identifier

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与其他类型( StringListExecutorService等)一样。 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.

相关问题 为什么我们不能从C#中的枚举实例访问枚举值? - Why can't we access enum values from an enum instance in C#? 为什么无法从枚举构造函数的 lambda 访问实例成员? - Why can't the instance member be accessed from the lambda of the enum constructor? 为什么enum的构造函数不能访问静态字段? - Why can't enum's constructor access static fields? 为什么我不能从另一个 class 访问对象的属性? (即使 Object 被传递给试图访问属性的 class) - Why can't I access the Object's properties from another class? (even when the Object was passed to the class that is trying to access the properties) Java:无法从另一个类调用实例的get方法,而不是实例化它 - Java: Can't call instance's get method from another class, than where it was instantiated 为什么我不能从Java中的专用枚举值访问静态最终成员 - Why can't I access static final members from a dedicated enum value in Java 为什么我不能在Kotlin中访问超类的枚举? - Why can't I access a superclass' enum in Kotlin? 为什么我不能从另一个 class 访问此方法? - Why can't I access this method from another class? 为什么我不能从另一个类访问方法 - why i can't access to method from another class 为什么我不能在 Java 的内部类中创建枚举? - Why can't I create an enum in an inner class in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM