Skip to main content

Generics using Enum in Java


I have an enum




public enum Days {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY



}



I want to make a class which can take values of type days. So i used Java Generics




public class State<T extend Days>



But there is an error




The type parameter T should not be bounded by the final type Days.
Final types cannot be further extended



How can i resolve this?


Source: Tips4allCCNA FINAL EXAM

Comments

  1. Don't use a generics bound. There's no need. Just use an unbounded type, like this:

    public class State<T> {
    public State(T startState) {
    // whatever
    }
    }


    And to use it:

    State<Days> dayState = new State<Days>(Days.SUNDAY);


    This is a straightforward typed class that doesn't need a bound.

    The only bound that might make sense is a bound to an enum:

    public class State<T extends Enum<T>> {
    public State(T startState) {
    // whatever
    }
    }


    This version requires that the generic parameter be an enum. With this version the above usage example would still compile, but this would not compile (for example):

    State<String> dayState = new State<String>("hello");


    because String is not an enum.

    ReplyDelete
  2. enums are final types, which means, you can not extend from them

    a generic like wants a Class as Parameter which is Days or an extended class, but the extended class is not possible

    so the only parameter possible is Days and you don't need a generic, if only one value is possible

    ReplyDelete
  3. You cannot extend enums in java (http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html towards the end). Thus is not possible.

    -IF you need your class to work only with your enum you don't need generics

    -if instead you need it to work with others enum (does it makes sense?) you don't need to specify the extend.

    What does exactly your State class do ?

    ReplyDelete
  4. The thing to do here is

    public enum Days{..}

    public class State { // no generic argument!
    ...
    }


    As it stands, you can't have State<T extends Days> because the only way to satisfy T extends Days is to have T be Days. That's what it means for Days to be final.

    So instead, you should make State not generic, and use Days directly everywhere you were trying to use T. You can't declare public class State<Days>, because then it'll try to treat Days as a type variable, not as the class Days, and that's not what you want.

    ReplyDelete
  5. This compiles fine for me under Java 6:

    public enum Days {
    SUNDAY,
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY
    }

    public class State<T extends Days> {
    T state;

    public State(T startState) {
    this.state = startState;
    }
    }

    private void test() {
    State<Days> state = new State<Days> (Days.MONDAY);
    }


    However, if you want your State object to change from one enum to another you would be better to use something like:

    public class State<S extends Enum<S>> {
    final ArrayList<S> states = new ArrayList<S>();
    private S state;

    public State(S startState) {
    states.addAll(EnumSet.allOf(startState.getClass()));
    this.state = startState;
    }

    public S next() {
    return state = states.get((state.ordinal() + 1) % states.size());
    }

    public S prev() {
    return state = states.get((state.ordinal() - 1 + states.size()) % states.size());
    }

    public S set(S newState) {
    return state = newState;
    }

    public S get() {
    return state;
    }
    }

    private void test() {
    State<Days> state = new State<Days> (Days.MONDAY);
    // ...
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex