querti
querti

Reputation: 77

Nested enumeration in an interface

I'm learning Java and I have some problems with implementation. My task is: I've been given automatically generated Java documentation and am supposed to create code according to it. According to the documentation, interface Inter1 is supposed to contain nested class Inter1.Directions. However closer information about Inter1.Directions states that it is actually an enumeration. I am having multiple syntax issues with this and I will try to explain them:

This is what my interface code currently looks like:

package newpack
public interface Inter1 {

    public static enum Direction {  

        E(0), N(1), W(2), S(3);
        Inter1.Direction[] values();
        Inter1.Direction valueOf(java.lang.String name);
        //are these methods only supposed to be declared, 
        //since this is technicaly not an interface?
    }
    //some method declarations....
}

This is what my enum implementation looks like:

package newpack
public static enum Inter1.Direction {

    //not sure if enumeration should be here if it's already in interface
    public static Inter1.Direction[] values() {
        //code...
    }

    public static Inter1.Direction valueOf(java.lang.String name) {
        //code...
    }
}

File that contains the enum is I think supposed to be named Inter1.Direction.java, however if I name the enum Inter1.Direction I get { expected error. If I remove Inter1. leaving only Direction I get identifier expected error. How should I modify my code so that I wouldn't get errors?

P.S.: I know there is probably better way to implement this, but I have to do it like this (or similarily).

EDIT:

I have moved the enum implementation to the inside of the interface and my code looks like this:

public interface Inter1 {

    public static enum Direction {  

        L, LU, U, RU, R, RD, D, LD;

        public static Inter1.Direction[] values() {
            //implementation
        }

        public static Inter1.Direction valueOf(java.lang.String name) {
            //implementation
        }
    }
//some method delarations
}

I now get

error: method values() is already defined in enum Direction

error: method valueOf(String) is already defined in enum Direction

Where's the problem now?

Upvotes: 0

Views: 192

Answers (1)

9000
9000

Reputation: 40884

You can put classes that are intimately intertwined with another class, and only make sense inside the context of that other class, inside the other class (inner classes).

Your Direction is likely has no use outside Inter1; it's the detail of the interface.

OTOH if Direction has any non-trivial implementation details, I would put it to a separate file. Currently it is a static inner class, so it does not have a reference to an outer instance to call its methods. Direction's behavior, AFAICT, should not depend on how a particular implementation of the interface works.

Instead of putting Direction into the scope of the interface, I would rather put it in the scope of the interface's package.

Your design could make sense if you had many similar interfaces, each with its own Direction enumeration, incompatible with others.

Upvotes: 1

Related Questions