concept local variable in category java

This is an excerpt from Manning's book Java SE 11 Programmer I Certification Guide.
As you can see, the variable avg, defined locally in the method getAverage, can’t be accessed outside it, in the method setAverage. The scope of this local variable, avg, is depicted in figure 4.18. The unshaded area marks where avg is accessible, and the shaded area is where it won’t be available. A local variable is only accessible after its declaration.
Figure 4.21 Comparison of the scope of method parameters and local variables
![]()
What happens when you assign a value to a local variable that has the same name as an instance variable? Does the instance variable reflect this modified value? This question provides the food for thought in this chapter’s first Twist in the Tale exercise. It should help you remember what happens when you assign a value to a local variable when an instance variable already exists with the same name in the class (answer in the appendix).
Twist in the Tale 4.2
The class Phone defines a local variable and an instance variable, phoneNumber, with the same name. Examine the definition of the method setNumber. Execute the class on your system and select the correct output of the class TestPhone from the given options:

This is an excerpt from Manning's book Modern Java in Action: Lambdas, streams, reactive and functional programming.
Java 8 and Java 9 both provided significant updates to Java. But as a Java programmer, it’s likely to be the Java 8 additions that affect you most on a daily small-scale-coding basis—the idea of passing a method or a lambda is rapidly becoming vital Java knowledge. In contrast, the Java 9 enhancements enrich our ability to define and use larger-scale components, be it structuring a system using modules or importing a reactive-programming toolkit. Finally, Java 10 is a much smaller increment compared to previous upgrades and consists of allowing type inference for local variables, which we discuss briefly in chapter 21, where we also mention the related richer syntax for arguments of lambda expressions due to be introduced in Java 11. At the time of writing, Java 11 is scheduled to be released in September 2018. Java 11 also brings a new asynchronous HTTP client library (http://openjdk.java.net/jeps/321) that leverages the Java 8 and Java 9 developments (details in chapters 15, 16, and 17) of CompletableFuture and reactive programming.
You’ve now seen how to create lambdas and where and how to use them. Next, we’ll explain some more advanced details: how lambdas are type checked by the compiler and rules you should be aware of, such as lambdas referencing local variables inside their body and void-compatible lambdas. There’s no need to fully understand the next section right away, and you may wish to come back to it later and move on to section 3.6 about method references.
Nonetheless, there’s a small twist. There are some restrictions on what you can do with these variables. Lambdas are allowed to capture (to reference in their bodies) instance variables and static variables without restrictions. But when local variables are captured, they have to be explicitly declared final or be effectively final. Lambda expressions can capture local variables that are assigned to only once. (Note: capturing an instance variable can be seen as capturing the final local variable this.) For example, the following code doesn’t compile because the variable portNumber is assigned to twice:

This is an excerpt from Manning's book Java 8 in Action: Lambdas, streams, and functional-style programming.
You’ve now seen how to create lambdas and where and how to use them. Next, we explain some more advanced details: how lambdas are type checked by the compiler and rules you should be aware of, such as lambdas referencing local variables inside their body and void-compatible lambdas. There’s no need to fully understand the next section right away, and you may wish to come back to it later and move on to section 3.6 about method references.
Nonetheless, there’s a small twist: there are some restrictions on what you can do with these variables. Lambdas are allowed to capture (that is, to reference in their bodies) instance variables and static variables without restrictions. But local variables have to be explicitly declared final or are effectively final. In other words, lambda expressions can capture local variables that are assigned to them only once. (Note: capturing an instance variable can be seen as capturing the final local variable this.) For example, the following code doesn’t compile because the variable portNumber is assigned to twice: