this keyword in Lambda expression in Java 8

  sonic0002        2014-06-01 03:57:05       18,257        2    

Since the release of Java 8, people are excited to see a big feature added to this language which is existing in other languages for a long time -- Lambda expression. The introduction of lambda expression in Java 8 gives people an easy way or a vertical to horizontal way to solve problems. There are many posts on the Internet which shows how to use lambda expression in Java, such as Lambda Quick Start and Java 8 Lambda Expressions Tutorial with Examples. In this post, we will only focus on the this keyword in lambda expression.

Unlike anonymous functions, a lambda expression can be considered as a simple code block when considering about variable scoping. Hence all scoping rules for code blocks also apply to lambda expression. This means a variable defined in the lambda expression can only be accessed within the lambda expression and the variables defined in the enclosing class can be accessed by the lambda expression as well, this also include the this keyword which refers to the enclosing class object.

In short, for anonymous class ‘this’ keyword resolves to anonymous class object, whereas for lambda expression ‘this’ keyword resolves to enclosing class object where lambda is written. Below is an example to demonstrate the difference.

SimpleInterface.java

public interface SimpleInterface {
	public String retrieveValue();
}

LambdaThis.java

public class LambdaThis {
	String value="default value";
	
	public SimpleInterface createLambdaInterface(){
		SimpleInterface si=()->{
			String value="lambda value";
			return this.value;
		};
		
		return si;
	}
	
	public SimpleInterface createAnonymousInterface(){
		SimpleInterface si=new SimpleInterface(){
			String value="anonymous value";
			
			public String retrieveValue(){
				return this.value;
			}
		};
		
		return si;
	}
	
	public static void main(String[] args){
		LambdaThis lt=new LambdaThis();
		
		SimpleInterface lambdaInterface=lt.createLambdaInterface();
		SimpleInterface anonymousInterface=lt.createAnonymousInterface();
		
		System.out.println("Lambda interface value : "+lambdaInterface.retrieveValue());
		System.out.println("Anonymous interface value : "+anonymousInterface.retrieveValue());
	}
}

The output from above program is :

Lambda interface value : default value
Anonymous interface value : anonymous value

The this keyword in the lambda expression refers to the LambdaThis object which has a property value "default value".

Also if you know how the compiler compiles the lambda expression, you will also get to know what the this refers to in lambda expression. Java compiler will convert the lambda expression into private method of the enclosing class. This is why the this keyword refers to the enclosing class object.

THIS  LAMBDA EXPRESSION  JAVA 8 

       

  RELATED


  2 COMMENTS


Anonymous [Reply]@ 2018-06-07 19:56:16

Nice example. But would you have a reference for the assertion "Java compiler will convert the lambda expression into private method of the enclosing class"?

Farhan [Reply]@ 2022-04-03 03:55:41

 

My rate is **********

Very good explanation. You picked single topic of this in lambda expression and explained it all in short descriptive way and also showed why this refer to enclosing object but not to lambda because java compilor converts lambda expression into private method of enclosing class hence this always refers to enclosing object of lambda expression. Thanks 



  RANDOM FUN

The ultimate tips about coding