“This” under the context of Java Lambda

 Today I wanna talk about how keyword “This” can have a totally different definition,
 under the context of lambda function on Java 8.
As you all may notice, lambda expression allow developer to pass method with specific parameter and type of result into another method as a parameter.
Usually it can be done on one of two ways, either utilizing functional interface which provide on Java 8 or construct a brand interface by ourself then construct a new instance of that interface and pass into the method.
In this example we constructed a custom interface for our usage.
Mock up interface to pass into method
b759d6b6-9c76-4e29-aef2-033c3a9f3b42
Within the lambda_this object, we have method call RunProcess and execute. RunProcess take integer and Process p as parameter and execute the process with given int value.
Then execute simply execute RunProcess with default value 10 and function to print this and incoming value.
ca65515e-61b8-4d27-8ec0-350db9c8e967
And the other example list below, we try to execute RunProcess outside of the object with a brand-new instance of Process along with implemented method. Of course: Printing out “this”.
a69a5608-ea10-4119-8bce-bb3fd1cf1829
This thing that surprise and motivate me to put on the post is how different of the printout result where call the same statement (2 lines of code) with-in / out the object body. Because when we dig into execute method, from what my understanding of lambda concept, the method { i-> {……} } would convert to a actual Process Interface with context feed into implemented method section. So should be something as below:
Hence, what I expecting is the keyword “this” is referring the “Process” instance created on compiling time which is the “Process” object. How ever when I try to run above code segment (Full version linked on bottom), “this” actually referring “lambda_this”, which is the object of the method execute.
Gd’ day
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s