Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limitations of Java Anonymous Classes compared to Objective-C Blocks

I'm just starting to wrap my head around first order functions and closures after discovering blocks in Objective-C. Java is another language where I've heard about closures (or lack thereof) and how anonymous classes make up for this somewhat.

I can definitely see the advantages of closures as blocks in Objective-C, but what are the limitations of anonymous Java classes? To what extent do they 'somewhat' make up for the lack of true closures?

like image 979
donalbain Avatar asked May 31 '11 19:05

donalbain


People also ask

What is the advantage of anonymous class in 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 inner class and anonymous class in Java?

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 use of anonymous object in Java?

Anonymous object in Java means creating an object without any reference variable. Generally, when creating an object in Java, you need to assign a name to the object. But the anonymous object in Java allows you to create an object without any name assigned to that object.

How many interfaces can an anonymous inner class implement?

A normal class can implement any number of interfaces but the anonymous inner class can implement only one interface at a time.


2 Answers

Java anonymous classes are really, really wordy. Apart from the vast amounts of boilerplate that you need just to define them, some of Java's design decisions mean that a lot of common tasks are much more verbose than in other languages. For example, importing mutable upvalues into the closure is a pain in the arse in Java.

Basically, Java doesn't support upvalues; instead they're simulated by passing them (by value) into the class via invisible parameters to the class's constructor. Because they're passed by value, modifying them inside the class won't affect the copy in the method that constructed the class, so the compiler makes you declare them final to avoid confusing yourself. e.g.:

Runnable function()
{
   final int i = 4;
   return new Runnable()
   {
       public void run()
       {
            System.out.println("i="+i);
            // can't modify i here
       }
   }
}

On occasions where you do need to modify the variable, for example in pretty much every case where closures would be useful, you have to cheat:

Runnable function()
{
   final int[] i = new int[1];
   i[0] = 4;
   return new Runnable()
   {
       public void run()
       {
            System.out.println("i="+i[0]);
            i[0] = i[0] + 1;
       }
   }
}

Here, i itself is still immutable, but because it points at a mutable object, I can change the contents of the object. (Naturally, in real life I'd use a class rather than an array, because using arrays is really ugly. And that makes it even more wordy.)

I gather that the next Java release is going to have syntactic sugar to make all this easier, but right now closure-centric programming is pretty cumbersome in Java. I find it's frequently easier to change the logic not to use closures, simply to allow me to keep the amount of code in use small enough to be comprehensible.

like image 157
David Given Avatar answered Nov 14 '22 21:11

David Given


I don't really know about the objective-C version of closures, but I know them from Smalltalk and Lua.

A closure is essentially a function which has access to a local variable of some other function/block (usually one in which the closure is syntactically nested). This way, the local variable can live longer than the block it is defined in. When you have multiple closures over the same variable, they can communicate using this variable.

Java's local classes (of which anonymous classes are a special case) support a limited version of this: They allow access to a final variable, i.e. a variable which can't change it's value. This is implemented by copying the value of this variable to the local class object on it's construction. You can emulate real closures by using a mutable object in this variable (in the simplest case a one-element array).

Additionally, the Java syntax for this is quite ugly, but it fits in with the rest of the language nicely, and allows static type safety (contrary to some shorter-syntax variants which are discussed).

like image 34
Paŭlo Ebermann Avatar answered Nov 14 '22 22:11

Paŭlo Ebermann