简体   繁体   中英

Separate Enum file won't accept variables

I've created an enum.java file and I get errors when creating variables. But in my other .java files, none of these errors appear. Within the enum Foo, the only thing causes no errors is if the Foo constructor takes no parameters and thatthere are no other variables within the enum.

The errors range from the String being an invalid modifier and the boolean to be deleted.

package com.foo.bar

public enum Foo
{
    String foo;
    boolean isBarable;

    Foo(String foo, boolean isBarable)
    {
        this.foo = foo;
        this.isBarable = isBarable;
    }
}

You're missing the most important element of the enum: the enum instances.

public enum Foo
{
    //  instances go here

    ;   // **** semicolon needed

    private String foo;
    private boolean isBarable;

    private Foo(String foo, boolean isBarable)
    {
        this.foo = foo;
        this.isBarable = isBarable;
    }
}

Shoot, just adding the semicolon alone would solve your compilation error, but without the enum instances, the enum is useless.

eg,

public enum Foo
{
    BAR("bar", true), BAZ("baz", false) ;

    private String foo;
    private boolean isBarable;

    private Foo(String foo, boolean isBarable)
    {
        this.foo = foo;
        this.isBarable = isBarable;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM