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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.