What variables can lambda expressions access? Does every usage of a lambda expression give rise to a new object?
- [Instructor] Hi, welcome to the third video…in this section, this video is about Capturing Values.…In this video, we're going to see that lambda expressions…can capture class fields and instance fields…and also local variables and then we're going to compare…anonymous classes and lambda expressions.…So first what do we mean by capturing something,…capturing fields?…We mean that the lambda expression can access…a static field of the enclosing class…and an instance field of the enclosing object.…
How does it do that, in particular,…how does it access instance fields?…It does so by secretly storing…the reference to the enclosing object.…Next, let me remind you…what are effectively final variables.…These are variables that are used as if they were final.…In other words, they're not reassigned.…And by the way, it's good practice…to just declare them final explicitly.…Why do we care about effectively final variables?…Well, because lambda expressions can also access…local variables of the methods they're located in…
Note: To get the best results from this course, you should be familiar with basic Java programming concepts, including lists, maps, and sets, and have a suitable IDE, such as Eclipse, NetBeans, or IntelliJ.
This course was created and produced by Packt Publishing. We are honored to host this training in our library.
- What is functional programming?
- What are functional interfaces?
- Writing lambda expressions
- Creating functional interfaces
- Composing functions
- Sequential data processing with streams
- Using parallel streams