← Back to list

Interaction of Lambda Expressions with Enclosing Code

Lambda expressions behave differently from anonymous classes when interacting with their surrounding code — especially when using this and…

Suraj · 2026-08-22 09:29 · 0 claps · 2.0 min read
#java-lambda-expression #modern-java #java-stream-api #clean-code
Open on Medium ↗
Wiki topics: 🌐 · Web Development ☁️ · DevOps & Cloud 📰 · Journalism & News

Interaction of Lambda Expressions with Enclosing Code

Lambda expressions behave differently from anonymous classes when interacting with their surrounding code — especially when using **this and `super`**.

The key idea is simple:

A lambda expression does not introduce a new object scope.

This directly affects what this and super mean inside a lambda.

'this’ in Anonymous Classes

An anonymous class creates a new object.

Therefore, this refers to the instance of that anonymous class.

class ProductService {

   private String name = "ProductService";

   void process() {

        Runnable task = new Runnable() {
            private String name = "AnonymousClass";
            @Override
            public void run() {
                System.out.println(this.name); // Output is: AnonymousClass
            }
        };
        task.run();
    }
}

Here:

this.name // refers to the member of the anonymous class.

So the output is: AnonymousClass

'this' in Lambda Expressions

A lambda expression is different.

It does not create a new object scope and cannot declare its own instance variables.

Therefore, this keeps the same meaning it has in the surrounding code.

class ProductService {

    private String name = "ProductService";

    void process() {
        Runnable task = () -> {
            System.out.println(this.name); // Output is : ProductService
        };
        task.run();
    }
}

Here:

this.name // refers to ProductService instance

So the output is: ProductService

A useful mental model is:

Anonymous Class
      ↓
creates its own object scope
      ↓
this → anonymous class instance
    Lambda
      ↓
no new object scope
      ↓
this → enclosing class instance

What About ‘super'?

The same principle applies to **super**.

Inside an anonymous class, super refers to the superclass of the anonymous class.

Inside a lambda expression, super has the same meaning as it does in the enclosing code.

Consider:

class Parent {

    void greet() {
        System.out.println("Hello from Parent");
    }
}

Now:

class Child extends Parent {

      void execute() {
        Runnable task = () -> {
            super.greet();
        };
        task.run();
    }
}

The lambda doesn’t introduce another class hierarchy.

Therefore:

super.greet();

means the same thing as if it were written directly inside Child:

Child
  ↓
super
  ↓
Parent

This difference exists because an anonymous class is an actual class with its own instance, while a lambda represents the implementation of behavior defined by a functional interface.

Key Takeaways

  • Anonymous classes create their own object scope.
  • Lambda expressions do not introduce a new object scope.
  • this inside an anonymous class refers to the anonymous class instance.
  • this inside a lambda refers to the enclosing class instance.
  • super inside a lambda behaves exactly as it does in the surrounding code.
  • Lambdas cannot declare their own instance variables.

In short:

Anonymous class → its own scope
Lambda          → enclosing scope

When you see this or super inside a lambda, think:

“Same meaning as the surrounding code.”

Continue Learning Java Lambdas 🚀

← Previous: Capturing Variables in Lambda Expressions | Next →: Working with Exceptions in Lambda Expressions


메타데이터
post_id
371a2b89428c
slug
interaction-of-lambda-expressions-with-enclosing-code-371a2b89428c
url
https://medium.com/@surajk.explains/interaction-of-lambda-expressions-with-enclosing-code-371a2b89428c
canonical_url
https://medium.com/@surajk.explains/interaction-of-lambda-expressions-with-enclosing-code-371a2b89428c
author_url
https://medium.com/@surajk.explains
status
ok
fetched_at
2026-09-03 04:44:24