Today's Question:  What weekend projects have you created?        GIVE A SHOUT

Technical Article => Programming =>  Java

this keyword in Lambda expression in Java 8

  sonic0002      2014-06-01 03:57:05      4,418    0

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.

public interface SimpleInterface {
	public String retrieveValue();

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.



Share on Facebook  Share on Twitter  Share on Google+  Share on Weibo  Share on Reddit  Share on Digg  Share on Tumblr    Delicious



No comment for this article.


Criteria of bad code

By sonic0002
The criteria of bad code is that you have a feeling of getting away from it as soon as possible every time when you see it. I have this feeling right at the moment.