Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 283083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:20:36+00:00 2026-05-12T05:20:36+00:00

Update: Thanks for everyone who helped out – the answer to this one lay

  • 0

Update: Thanks for everyone who helped out – the answer to this one lay in what I wasn’t noticing in my more complex code and what I didn’t know about the Java5 covariant return types.

Original Post:

I’ve been playing around with something this morning. While I know that I could tackle this whole problem differently, I’m finding myself obsessed with figuring out why it isn’t working the way that I was expecting. After spending some time reading around on this, I find I’m no closer to understanding, so I offer it up as a question to see if I’m just being stupid or if there is really something I don’t understand going on here.

I have created a custom Event hierarchy like so:

public abstract class AbstractEvent<S, T extends Enum<T>>
{
    private S src;
    private T id;

    public AbstractEvent(S src, T id)
    {
        this.src = src;
        this.id = id;
    }

    public S getSource()
    {
        return src;
    }

    public T getId()
    {
        return id;
    }
}

With a concrete implementation like so:

public class MyEvent
extends AbstractEvent<String, MyEvent.Type>
{
    public enum Type { SELECTED, SELECTION_CLEARED };

    public MyEvent(String src, Type t)
    {
        super(src, t);
    }
}

And then I create an event like so:

fireEvent(new MyEvent("MyClass.myMethod", MyEvent.Type.SELECTED));

Where my fireEvent is defined as:

protected void fireEvent(MyEvent event)
{
    for(EventListener l : getListeners())
    {
        switch(event.getId())
        {
            case SELECTED:
                l.selected(event);
                break;
            case SELECTION_CLEARED:
                l.unselect(event);
                break;
         }
    }
}

So I thought that this would be pretty straightforward but it turns out that the call to event.getId() results in the compiler telling me that I cannot switch on Enums, only convertible int values or enum constants.

It is possible to add the following method to MyEvent:

public Type getId()
{
    return super.getId();
}

Once I do this, everything works exactly as I expected it to. I’m not just interested in finding a workaround for this (because I obviously have one), I’m interested in any insight people might have as to WHY this doesn’t work as I expected it to right off the bat.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:20:36+00:00Added an answer on May 12, 2026 at 5:20 am

    Yishai is right, and the magic phrase is “covariant return types” which is new as of Java 5.0 — you can’t switch on Enum, but you can switch on your Type class which extends Enum. The methods in AbstractEvent that are inherited by MyEvent are subject to type erasure. By overriding it, you’re redirecting the result of getId() towards your Type class in a way that Java can handle at run-time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

UPDATE: Thank you everyone who helped out. I hope the myriad of solutions on
UPDATE: Solved. Thanks BusyMark! EDIT: This is revised based on the answer below from
at the first I would like to thanks every one who helped me one
update: I mistyped 2 variables...so embarrassing. thanks everyone for the effort! sorry i find
UPDATE: I've been playing around with this more, and it seems like tmux's clear-history
THIS PROBLEM IS NOW SOLVED. THANK YOU TO EVERYONE WHO REPLIED. Hello, I am
UPDATE & SOLUTION : Everyone, for whoever has this problem in the future, I
Update Thanks to Marc's help the AlphaPagedList class is now available on CodePlex if
Update: Thanks guys, I didn't realize it was very close to zero but not
Update: I reported this as a bug to Apple and they fixed it! All

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.