Lambdas vs Anonymous Inner Classes

Posted on Updated on

Anonymous inner classes are used by java programmer as “ad hoc” functionality i.e where and when they are needed. Since Java 8, we can use lambda expressions instead of anonymous inner classes. It might seems that both inner classes and lambda expressions are similar as they both are used to implement ad hoc functionality. However there are some differences between lambda expression and inner classes which are as follow.

1)  Syntax

Syntax of both lambda expression and anonymous inner classes are quite different. The notation for Anonymous inner classes is detailed while for lambda expression, it is concise. Here is the example to demonstrate difference in syntax between anonymous inner classes and lambdas.

2) Variable Binding

Anonymous inner classes have support to access variables from the enclosing context. It has access to all final variables of its enclosing context. Below is an example to demonstrate this

The anonymous inner class above has access to cnt variable because it is defined as final in enclosing context.
Lambda Expression too has support to variable binding as demonstrated below.

However, as you can see in above example cnt variable is not a final variable still it can be used inside lambda expresssion. Also, cnt++ throws compilation error. The difference is that the variables from the enclosing context that are used inside a lambda expression are implicitly final. i.e compiler automatically add final keyword before cnt variable.

Note : Since Java 8, the explicit final declaration is no longer needed for anonymous inner classes as well. Both lambda expressions and anonymous inner classes are treated the same regarding variable binding.

3) Scoping

An anonymous inner class is a class, which means that it has scope for variable defined inside the inner class as demonstrate in below example.

However, lambda expression is not a scope of its own, but is part of the enclosing scope. Here is the example to demonstrate the differences

Both anonymous inner classes and lambda expression have cnt as enclosing context variable. Since anonymous inner classes has its own scope we can define cnt variable inside run method. However lambda expression is not a scope of its own we can’t define cnt variable again in lambda expression. In case we try to do so, code will show compilation error as “Lambda expression’s local variable cnt cannot redeclare another local variable defined in an enclosing scope“.

Similar rule applies for super and this keyword when using inside anonymous inner class and lambda expression. In case of anonymous inner class this keyword refers to local scope and super keyword refers to the anonymous class’s super class. While in case of lambda expression this keyword refers to the object of the enclosing type and super will refer to the enclosing class’s super class.

4) Performance

At runtime anonymous inner classes require class loading, memory allocation and object initialization and invocation of a non-static method while lambda expression is pure compile time activity and don’t incur extra cost during runtime. So performance of lambda expression is better as compare to anonymous inner classes.

Stay tuned for more updates !

One thought on “Lambdas vs Anonymous Inner Classes

    Nikhil said:
    December 1, 2016 at 6:45 pm

    appreciate your work!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.