Вы не можете дать имя своему анонимному классу, поэтому он называется «анонимный». Единственный вариант, который я вижу, - ссылаться на переменную final
из внешней области видимости вашего Callable
// Your outer loop
for (;;) {
// Create some final declaration of `e`
final E e = ...
Callable<E> c = new Callable<E> {
// You can have class variables
private String x;
// This is the only way to implement constructor logic in anonymous classes:
{
// do something with e in the constructor
x = e.toString();
}
E call(){
if(e != null) return e;
else {
// long task here....
}
}
}
}
Другой вариант - создать локальный класс (не анонимный класс), например:
public void myMethod() {
// ...
class MyCallable<E> implements Callable<E> {
public MyCallable(E e) {
// Constructor
}
E call() {
// Implementation...
}
}
// Now you can use that "local" class (not anonymous)
MyCallable<String> my = new MyCallable<String>("abc");
// ...
}
Если вам нужно больше, создайте обычный MyCallable
класс ...