Topic: 1z0-819 topic 1 question 206

Given:



What is the result?

A.
Oracle
B.
An exception is thrown at run time.
C.
Java
D.
Compilation error

Re: 1z0-819 topic 1 question 206

tested

Re: 1z0-819 topic 1 question 206

variables in lambdas must be a final or effectively final. If s is modified then code not compile

Re: 1z0-819 topic 1 question 206

right answer is D

Local variables decalred in the enclosing method, including its formal parameters, can be accessed in a lambda expression provided and they are effectively final
This one :

String s = "Oracle";
Runnable r = () -> {
  System.out.println(s);
};
//s = "Java";
Thread t = new Thread(r);
t.start()

will be ok

Re: 1z0-819 topic 1 question 206

that is ok, variables in lambdas must be a final or effectively final. If s is modified then code not compile