Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between anonymous classes in Java and closures?

It looks like anonymous class provides the basic functionality of closure, is that true?

like image 823
user705414 Avatar asked Jun 20 '11 14:06

user705414


People also ask

What are anonymous classes Java?

Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once.

What is the difference between anonymous class and inner class?

A local inner class consists of a class declared within a method, whereas an anonymous class is declared when an instance is created. So the anonymous class is created on the fly or during program execution.

What is the difference between lambda vs anonymous class?

Anonymous class is an inner class without a name, which means that we can declare and instantiate class at the same time. A lambda expression is a short form for writing an anonymous class. By using a lambda expression, we can declare methods without any name.

What does anonymous mean in Java?

In Java, a class can contain another class known as nested class. It's possible to create a nested class without giving any name. A nested class that doesn't have any name is known as an anonymous class. An anonymous class must be defined inside another class. Hence, it is also known as an anonymous inner class.


1 Answers

There is almost no difference. In fact the there is an old saying about closures and objects. Closures are the poor man's object, and objects are the poor man's closure. Both are equally powerful in terms of what they can do. We are only arguing over expressiveness.

In Java we are modeling closures with Anonymous Objects. In fact a little history here is that originally Java had the ability to modify the outward scope without the use of final. This works and worked fine for Objects allocated in the local method scope, but when it comes to primitives this caused lots of controversy. Primitives are allocated on the stack so in order for them to live past the execution of the outer method Java would have to allocate memory on the heap and move those members into the heap. At that time people were very new to garbage collection and they didn't trust it so the claim was Java shouldn't allocate memory without explicit instruction from the programmer. In efforts to strike a compromise Java decided to use the final keyword.

http://madbean.com/2003/mb2003-49/

Now the interesting thing is that Java could remove that restriction and make use of the final keyword optional now that everyone is more comfortable with the garbage collector and it could be completely compatible from a language perspective. Although the work around for this issue is simple to define instance variables on your Anonymous Object and you can modify those as much as you wish. In fact that could be an easy way to implement closure style references to local scope by adding public instance variables to the anonymous class through the compiler, and rewriting the source code to use those instead of stack variables.

public Object someFunction() {    int someValue = 0;     SomeAnonymousClass implementation = new SomeAnonymousClass() {        public boolean callback() {            someValue++;        }    }    implementation.callback();    return someValue; } 

Would be rewritten to:

 public Object someFunction() {    SomeAnonymousClass implementation = new SomeAnonymousClass() {        public int someValue = 0;         public boolean callback() {            someValue++;        }    }    implementation.callback();     // all references to someValue could be rewritten to     // use this instance variable instead.    return implementation.someValue; } 

I think the reason people complain about Anonymous inner classes has more to do with static typing vs dynamic typing. In Java we have to define an agreed upon interface for the implementor of the anonymous class and the code accepting the anonymous class. We have to do that so we can type check everything at compile time. If we had 1st class functions then Java would need to define a syntax for declaring a method's parameters and return types as a data type to remain a statically typed language for type safety. This would almost be as complex as defining an interface. (An interface can define multiple methods, a syntax for declaring 1st class methods would only be for one method). You could think of this as a short form interface syntax. Under the hood the compiler could translate the short form notation to an interface at compile time.

There are a lot of things that could be done to Java to improve the Anonymous Class experience without ditching the language or major surgery.

like image 89
chubbsondubs Avatar answered Sep 21 '22 04:09

chubbsondubs