Closure in Java

A closure is an inner function that has access to the outer function even after the scope of outer function.This is a common programming concept in JavaScript, but we can achieve the same thing in java with the advent of JDK8 and functional programming.

here is a small java program which explains the closure concept in java,

 * This program explains the closure in java. This is achieved by using functional programming in JDK 8.
package lambdas;

 * @author prabhu kvn
public class LambdaClosureConcept {

	public LambdaClosureConcept() {

	public static void main(String[] args) {
		LambdaClosureConcept obj = new LambdaClosureConcept();

	private void execute() {
		LambdaClosure fn = getFunction();


	 * The "local" variable which got trapped in closure function.
	 * @return
	private LambdaClosure getFunction() {
		int local = 89;
		LambdaClosure fn = (int i) -> {
			return i * local;

		return fn;



// Functional interface
interface LambdaClosure {

	int changeVal(int i);

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