Got a little puzzle for a true Java Generics specialist... ;)
Let\'s say I have the following two interfaces:
interface Processor {
void process(Foo
Here's your code fully "genericized", but with one slight change: The INSTANCE variable is not static.
interface Processor> {
void process(T foo);
}
interface Foo> {
Processor getProcessor();
}
static class SomeProcessor> implements Processor {
final SomeProcessor INSTANCE = new SomeProcessor();
@Override
public void process(T foo) {
// it will only ever be a SomeFoo if T is SomeFoo
}
}
class SomeFoo implements Foo {
@Override
public Processor getProcessor() {
return new SomeProcessor().INSTANCE;
}
}
No compiler errors or warnings.
INSTANCE was made an instance variable because class types do not make it through to static anything. If you really only wanted one INSTANCE, use the singleton pattern on the class.