Neo
Neo

Reputation: 391

Access enum inside an interface

I am completely new to Java. I have an interface that has a few methods that I need to implement. Inside the interface, there is a class that has enums that I need to access.

It looks like this:

public interface Operations{
    //some function names that I have to implement
    public static enum ErrorCodes{
        BADFD;
        NOFILE;
        ISDIR;
        private ErrorCode{
        }
    }
}

In my implementation, when I try to access ErrorCodes.BADFD it gives me error. I do not know the right way to access it. Also, what is the empty private ErrorCode{} called. Is it the constructor? What does it do?

EDIT : added uppercase 'o' to enum name

Upvotes: 2

Views: 2034

Answers (3)

Raghu K Nair
Raghu K Nair

Reputation: 3942

Here is the corrected one

public interface Operations{
//some function names that I have to implement
public static enum ErrorCodes{
    BADFD,
    NOFILE,
    ISDIR;
    private ErrorCodes(){}
}

Upvotes: 2

Anthony Raymond
Anthony Raymond

Reputation: 7872

I believe you have to call the enum this way :

Operations.ErrorCode.BADFD

Because ErrorCode is an inner enum of the Operation interface.

I noticed few typo problems, take a look at this code :

public interface Operations {
    //some function names that I have to implement
    public static enum ErrorCode {
        BADFD,
        NOFILE,
        ISDIR;
        private ErrorCode() {
        }
    }
}

Upvotes: 1

rgettman
rgettman

Reputation: 178343

First, let's correct your malformed code:

// lowercase "interface"
// Usually interfaces and classes are capitalized
public interface Operations{
    // Singular to match the rest of the code and question.
    public static enum ErrorCode{
        // commas to separate instances
        BADFD,
        NOFILE,
        ISDIR;
        // Parameterless constructor needs ()
        private ErrorCode() {
        }
    }
}

To reference ErrorCode outside of the interface, you must qualify it with ErrorCode's enclosing interface, Operations.

Operations.ErrorCode code = Operations.ErrorCode.BADFD;

Upvotes: 7

Related Questions