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?
Your classname is AStats.Result
, not Result
:
public class AStats implements Stats<Item, AStats.Result> {
...
}
I don't think that returning an instance of generic, inner type is a bad idea.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With