From the course: Functional Programming with Java

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Closure

Closure - Java Tutorial

From the course: Functional Programming with Java

Start my 1-month free trial

Closure

- [Instructor] In a previous video, we talked about how in Java it's possible to return functions from other functions, and we saw some possible applications of doing this in our code base. So returning functions from other functions leads us to another very important concept in Java, and this is something called closure. So what is closure exactly? Well, basically, closure means that when we define a function that returns another function, the function that we return still has access to the internal scope of the function that returned it. So to see exactly what I mean here, let's code out an example that demonstrates closure. The first thing we're going to do is we're going to create a function called createGreeter. This is going to be pretty similar to what we just saw when we looked at returning functions. So we're going to say NoArgFunction, and this function is going to return a string, and we'll call it createGreeter, and what this function is going to do is it's simply going to…

Contents