Reputation: 9579
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?
Upvotes: 4
Views: 192
Reputation: 44376
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.
Upvotes: 8