Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inner static enum as generic type?

I am learning and experimenting with Java generics and come up with following piece of code which does not compile as expected. Result cannot be resolved. I stands for input, O for output.

public interface Stats<I, O> {
    O addItem (int index, I item);
}

public class AStats implements Stats<Item, Result> {
    public static enum Result {
        SUCCESS1,
        SUCCESS2,
        ERROR;
    }

    @Override
    public Result addItem (int index, Item item) {
        //valid code
    }
}
  • Is there more elegant solution than declaring Result in a separate file?

  • Is it bad in general to have a method which returns an instance of generic type?

like image 987
Nikolay Kuznetsov Avatar asked Feb 18 '23 02:02

Nikolay Kuznetsov


1 Answers

  1. Your classname is AStats.Result, not Result:

    public class AStats implements Stats<Item, AStats.Result> {
       ...
    }
    
  2. I don't think that returning an instance of generic, inner type is a bad idea.

like image 59
Crozin Avatar answered Feb 27 '23 16:02

Crozin