Are there any memory or speed issues with creating many anonymous inner classes? In the following code, I like the succinctness of experiment1, but I am concerned that including anonymous class creation in a highly iterated loop would make it lose out to experiment2 for some dimension(s) of performance.
public class Test {
interface Task {
public void do();
}
public void experiment1 {
for (int i = 0; i < 1000000; i++) {
Task inner = new Task() {public void do() {/* stuff */}}
}
}
public void experiment2 {
class InnerClass implements Task {
public void do() {/* stuff */}
}
for (int i = 0; i < 1000000; i++) {
Task inner = new InnerClass();
}
}
}