Skip to content

Commit 7b506b7

Browse files
committed
feat(lambda-vs-anonymous): implement Employee functional interface with lambda and anonymous inner class
What - Created `Employee` as a `@FunctionalInterface` with a single abstract method `String getSalary()`. - Implemented two different approaches in `Main.doSomething()`: 1. **Lambda Expression** - Provides inline functional implementation of `Employee`. - Uses a local variable (`int x = 10`) inside the lambda body. - Accesses and modifies the outer class’s instance variable `a = 2 → 3`. - Returns a fixed salary string `"100"`. 2. **Anonymous Inner Class** - Provides a class-like implementation of `Employee` directly inside method scope. - Defines its own instance variable (`int x = 10`). - Accessed via `this.x` since `this` refers to the anonymous class instance. - Returns `"100"` after printing its own `x`. Why - Demonstrates key differences between **lambda expressions** and **anonymous inner classes**: - Lambdas are functions, not classes, so they cannot declare instance variables. - Lambdas capture variables from the enclosing scope (`a` from outer class, `x` as local var). - Anonymous inner classes are full classes and can declare their own instance fields (`this.x`). - Clarifies how variable scoping and `this` resolution differ in both approaches. Logic 1. `@FunctionalInterface` ensures only one abstract method (`getSalary()`), making it compatible with lambdas. 2. Lambda version: - Local `x` is method-local, not tied to the class. - Outer instance variable `a` is accessible and can be modified. - `this` would refer to the enclosing class (`Main`). 3. Anonymous inner class version: - Declares its own `int x` as a field. - `this` refers to the anonymous class itself, so `this.x` resolves to its own instance variable. 4. Both implementations return the same result (`"100"`) but with different scoping and resolution behavior. Key Takeaways ✔ `@FunctionalInterface` + lambda → concise, function-like behavior. ✔ Anonymous inner class → heavier but allows class-specific fields/methods. ✔ `this` in lambda → enclosing class. ✔ `this` in anonymous inner class → the anonymous inner class itself. ✔ Instance variable (`a`) accessible from lambda, but not from inner class unless explicitly referenced. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent ede4d69 commit 7b506b7

File tree

1 file changed

+33
-0
lines changed
  • Java 8 Crash Course/Lambda Expression/Local Variable Inside Lambda Expression/src

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class Main {
2+
int a = 2; // instance variable (allowed to modify inside lambda).
3+
4+
public static void main(String[] args) {
5+
new Main().doSomething();
6+
}
7+
8+
private void doSomething() {
9+
// Using lambda to implement Employee interface.
10+
// below is Function not class.
11+
Employee employee = () -> {
12+
int x = 10;
13+
//Local Variable.
14+
System.out.println(x); //this is inside Function not class, that's why we not use (this.x) here
15+
16+
a = 3;
17+
return "100";
18+
};
19+
20+
//Below We are doing this using Anonymous Inaner class.
21+
// Below is Class.(Anonymous Inaner class).
22+
Employee employee1 = new Employee() {
23+
int x = 10;
24+
//Ye abhi class mai hai anonymous, so this is an instance variable.
25+
26+
@Override
27+
public String getSalary() {
28+
System.out.println(this.x); //"x" is instances variable.
29+
return "100";
30+
}
31+
};
32+
}
33+
}

0 commit comments

Comments
 (0)