Index Page : Link
Donate : Link
Medium Blog : Link
Applications : Link
Thread Example using Anonymous Inner Class :
package com.codeFactory.anonymousClass;
public class Test {
public static void main(String... args) {
/* Child Thread start */
Runnable r = new Runnable() {
@Override
public void run() {
for(int i=0; i<5; i++) {
System.out.println("Child Thread");
}
}
};
/* Child Thread end */
Thread t = new Thread(r);
t.start();
/* Main Thread start */
for(int i=0; i<5; i++) {
System.out.println("Main Thread");
}
/* Main Thread end */
}
}
Output :
Main Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Main Thread
Main Thread
Main Thread
Main Thread
Thread Example using Lambda (λ) Expression :
package com.codeFactory.anonymousClass;
public class Test {
public static void main(String... args) {
/* Child Thread start */
Runnable r = () -> {
for(int i=0; i<5; i++) {
System.out.println("Child Thread");
}
};
/* Child Thread end */
Thread t = new Thread(r);
t.start();
/* Main Thread start */
for(int i=0; i<5; i++) {
System.out.println("Main Thread");
}
/* Main Thread end */
}
}
Output :
Child Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Child Thread
Child Thread
Child Thread
Child Thread
. . . . .
Anonymous Inner Class != Lambda (λ) Expression :
(1) Anonymous Inner Class that extends concrete class
class Test {
}
Test t = new Test() {
};
(2) Anonymous Inner Class that extends abstract class
abstract class Test {
}
Test t = new Test() {
};
(3) Anonymous Inner Class that implements an interface which contains multiple methods
interface Test {
public void m1();
public void m2();
public void m3();
}
Test t = new Test() {
@Override
public void m1() {
}
@Override
public void m2() {
}
@Override
public void m3() {
}
};
(4) Anonymous Inner Class that implements an interface which contains only one abstract method
// with Anonymous Inner Class
interface Test {
public void m1();
}
Test t = new Test() {
@Override
public void m1() {
}
};
// with λ-Expression
interface Test {
public void m1();
}
Test t = () -> {
};
Note : λ-Expression is only possible in last scenario. In first 3 scenario λ-Expression is not possible.
. . . . .
Example 1 :
package com.codeFactory.anonymousClass;
interface Interf {
public void m1();
}
public class Test {
int x = 10;
public void m2() {
Interf i = new Interf() {
int x = 20; // instance variable
@Override
public void m1() {
System.out.println(this.x);
System.out.println(Test.this.x);
}
};
i.m1();
}
public static void main(String... args) {
Test t = new Test();
t.m2();
}
}
Output :
20
10
Example 2 :
package com.codeFactory.anonymousClass;
interface Interf {
public void m1();
}
public class Test {
int x = 10;
public void m2() {
Interf i = () -> {
int x = 20;
System.out.println(x);
System.out.println(this.x);
};
i.m1();
}
public static void main(String... args) {
Test t = new Test();
t.m2();
}
}
Output :
20
10
– Inside Anonymous inner class we can declare instance variable. Inside Lambda (λ) Expression it is not possible to declare instance variable, whatever variables declared inside Lambda (λ) Expression simply local variables only.
– Inside Anonymous inner class
thisalways refers current inner class object. Inside Lambda (λ) Expressionthisalways refers current outer class object only.– Lambda (λ) Expression came to replace Anonymous inner classes is wrong statement.
– Anonymous inner class is more powerfull than Lambda (λ) Expression. A particular case of Anonymous inner class we can replace with Lambda (λ) Expression.
Example 3 :
package com.codeFactory.anonymousClass;
interface Interf {
public void m1();
}
public class Test {
int x = 10;
public void m2() {
int y = 20;
Interf i = () -> {
System.out.println(x); // ✓
System.out.println(y); // ✓
x = 111; // ✓
y = 222; // X
};
i.m1();
}
public static void main(String... args) {
Test t = new Test();
t.m2();
}
}
CE : local variables referenced from a lambda expression must be final or effectively final
. . . . .
Advantages of Lambda (λ) Expression :
- We can enable functional programming in Java
- We can reduce length of the code so that readability wll be improved
- We can resolve complexity of Anonymous Inner Class until some extent
- We can handle procedures/functions just like values
- We can pass procedures/functions as arguments
- Easier to use updated APIs and libraries
- Enable support for parallel processing
. . . . .
Difference between Anonymous Inner Class and Lambda Expression :
| Anonymous Inner class | Lambda Expression |
| It’s a class without name | It’s a method without name (anonymous function) |
| Anonymous inner class can extend Abstract and concrete classes | Lambda (λ) expression can’t extend Abstract and concrete classes |
| Anonymous inner class can implement an interface that contains any number of Abstract methods | Lambda (λ) expression can implement an interface which contains single abstract method (Functional Interface) |
| Inside anonymous inner class we can declare instance variables | Inside Lambda (λ) Expression we can’t declare instance variables, whatever the variables declared are simply acts as local variables |
| Anonymous inner classes can be instantiated | Lambda (λ) expressions can’t be instantiated |
| Inside anonymous inner class this always refers current anonymousInner class object but not outer classObject | Inside Lambda (λ) Expression this always refers current outer class object. That is enclosing class object |
| Anonymous inner class is the best choice if we want to handle multiple methods | Lambda (λ) expression is the best choice if we want to handle interface with single abstract method (Functional Interface) |
| In the case of anonymous inner class at the time of compilation a separate dot class file will be generated (outerclass$1.class) | At the time of compilation no dot class file will be generated for Lambda (λ) expression. It simply convertsin to private method outer class |
| Memory allocated on demand whenever we are creating an object | Reside in permanent memory of JVM (Method Area) |


Just what I was looking for, regards for posting.
LikeLiked by 1 person
Thank you Mahalia Opdyke
Please check new blog https://bit.ly/31989Yt
You’ll find best Android Apps on new blog
Download | Comment | Share
Buy Me A Coffee if you want, Find “Donate” link in Menu
LikeLike
Fantastic goods from you, man. I’ve understand your stuff previous to and you’re just extremely excellent. I actually like what you’ve acquired here, certainly like what you’re stating and the way in which you say it. You make it entertaining and you still care for to keep it sensible. I can’t wait to read much more from you. This is actually a terrific site.
LikeLiked by 1 person
Thank you Alexis Egan
Please check new blog https://bit.ly/31989Yt
You’ll find best Android Apps on new blog
Download | Comment | Share
Buy Me A Coffee if you want, Find “Donate” link in Menu
LikeLike