=== Conclusion ====
found a good read at https://softwareengineering.stackexchange.com/a/149569 which states
Current GC algorithms are actually optimized for creating many many small objects that are short lived, So I think using anonymous inner class a lot in project would not be a big deal regarding to performance*
========================================================================
Because function is not the first class citizen in current Java(Java7), using anonymous inner class seems the only way to implement full async application.
I know it will bring larger memory footprint and burden garbage collector in some extent, but I don't know how serious it could be? Recently my colleague argued with me because my code was written in functional style by leveraging anonymous inner class, his objection was all about performance. Though I don't agree, I can't cite any example to prove myself. I know groovy is implementing closure all using anonymous class, but groovy does have poorer performance than java(of course anonymous should only take part of responsibility, as groovy heavily uses reflection as well).
so I wonder in real world, is there any project dropping anonymous class just because performance? how about UI framework like swing? Is it using anonymous class massively?
without anonymous, I can't imagine how to implement async elegantly in java. our project already uses a very ugly way to make class method work as function pointer. I hate that much and want to convince people anonymous class is the right way to go.
My Example:
// basically, I use Completion interface to make normal java methods work in async manner
public interface Completion {
void success();
void fail(String reason);
}
void methodA(Completion completion) {
do_some_business_by_calling_remote_service ....
when remote_service_ack_success:
completion.success();
else:
completion.fail(remote_service_error);
}
void methodB() {
methodA(new Completion() {
public void success() {
continue to do something;
}
public void fail(String err) {
handle error
}
});
}