Open In App

Closures in Java with Examples

Last Updated : 05 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A method is a collection of statements that perform some specific task and return the result to the caller. A method can perform some specific task without returning anything. Methods allow us to reuse the code without retyping the code. In Java, every method must be part of some class that is different from languages like C, C++, and Python. In this article, we will understand one type of function named closures. 

Lambda Expression in Java

Before getting into the closures, let’s first understand what a lambda expression is. A lambda expression basically expresses instances of the functional interfaces(An interface with a single abstract method is called a functional interface). An example is java.lang.Runnable

Functionalities of Lambda Expressions

Lambda expressions implement only abstract functions and therefore implement functional interfaces. Lambda expressions are added in Java 8 and provide the below functionalities: 

  1. Enable to treat functionality as a method argument, or code as data.
  2. A function that can be created without belonging to any class.
  3. A lambda expression can be passed around as if it was an object and executed on demand.

The main limitation of the lambda expression is that the scope for the lambda expressions will only be final. That is, we can’t change the value of the variables in the lambda expressions. Suppose we define a lambda expression in which we increment the value of a variable, it simply throws an error. In order to solve this error, the closures have been defined. 

Examples of Lambda Expression

Example 1: In this example, we will first implement a lambda expression without a parameter or an argument. 

Java




// Java program to demonstrate
// how a closure is implemented
// using lambda expressions
import java.io.*;
 
// Defining an interface whose
// implementation is given in
// the lambda expression.
// This uses the concept of
// closures
interface SalutationInterface {
    public String salHello();
}
 
// Driver Class
class GFG {
    // Driver code
    public static void main(String[] args)
    {
        // Lambda Expression
        SalutationInterface obj = () ->
        {
            return "Hello, GFGians!";
        };
 
        // Calling the above interface
        System.out.println(obj.salHello());
    }
}


Output

Hello, GFGians!

Example 2: In this example, we will understand how to implement the lambda expression which takes multiple parameters. 

Java




// Java program to demonstrate
// how a closure is implemented
// using lambda expressions
import java.io.*;
 
// Defining an interface whose
// implementation is given in
// the lambda expression.
// This uses the concept of
// closures
interface concatStrings {
    public String concat(String a, String b);
}
 
// Driver Class
class GFG {
    // Driver code
    public static void main(String[] args)
    {
        // Lambda Expression
        concatStrings s = (s1, s2) -> s1 + s2;
 
        System.out.println(s.concat("Hello, ", "GFGians!"));
    }
}


Output

Hello, GFGians!

Closures in Java

Closures are the inline-function valued expressions which means that they are the class functions with bounded variables. Closures can be passed to another function as a parameter. A closure gives us access to the outer function from an inner function. But as of Java 1.6, Java does not rely on closures or Java does not have closures. Also, anonymous inner classes are not closures in Java. Using closures helps in data privacy and currying (currying means breaking a function with many parameters as a number of functions in a single argument). Though the concept of closure is much more well-defined in Javascript, closures can still be implemented using lambda expressions.

Java Closures Syntax

The syntax to implement a closure is: 

(argument_list) -> {func_body}

Examples of the Java Closures

Now, let’s understand how to implement the closures with examples:

Example 1: In this example, we are going to use a custom functional interface to display the month from the number provided. 

Java




// Java program to demonstrate
// how a closure is implemented
// using lambda expressions
import java.io.*;
 
// Defining an interface whose
// implementation is given in
// the lambda expression.
// This uses the concept of
// closures
interface NumToMonth {
    public String convertToMonth(int x);
}
 
// Driver Class
class GFG {
    // Driver code
    public static void main(String[] args)
    {
        // Lambda Expression
        NumToMonth obj = new NumToMonth() {
            String[] months
                = { "Jan", "Feb", "Mar", "Apr",
                    "May", "Jun", "Jul", "Aug",
                    "Sep", "Oct", "Nov", "Dec" };
 
            public String convertToMonth(int n)
            {
                return (n > 0 && n <= months.length)
                    ? months[n - 1]
                    : null;
            };
        };
 
        System.out.println(obj.convertToMonth(8));
    }
}


Output

Aug

Example 2:

Java




// Java Pogram to implement
// to calculate the result
// using Closure and lamda methods
@FunctionalInterface
public interface Operation {
    void operate(int n);
}
 
ClosureExample.java package com.javatpoint;
 
public class GFG {
    public static void main(String args[])
    {
        int a = 12;
        int b = 88;
 
        // implementation of closure in lambda expression
        temp(a, new Operation() {
            // overrides the operate() method
            @Override public void operate(int n)
            {
                // prints the result
                System.out.println("Result is: " + (n + b));
            }
        });
    }
 
    private static void temp(int i, Operation op)
    {
        op.operate(i);
    }
}


Output:

Result is: 100

Closure vs Lambda

Both lambda and closure are quite similar to each other the difference can be understood using the difference between a class and an instance of the class. A Class exists only in the source code, and it doesn’t exist during the runtime.  There are only objects of the class type during the runtime. Similarly, Closures are to lambdas as objects are to classes.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads