ProgrammingHardGATE CS · TCS NQT Prime · Product Company SDE

A lambda expression captures a local variable from its enclosing method. What restriction does Java place on that variable?

  1. A. It must be declared volatile
  2. B. It must have a primitive type
  3. C. It must be declared static
  4. D. It must be effectively final: never reassigned anywhere after its initial assignment

Correct answer: D. It must be effectively final: never reassigned anywhere after its initial assignment

Java only allows a lambda to capture local variables that are final or effectively final, since the lambda may outlive the stack frame the variable lives in.

More OOP with C++ and Java questions

Exams that ask this topic