Lambda Expressions Require "Final" or "Effectively Final" Variables
The error message "Variable used in lambda expression should be final or effectively final" indicates that a variable used within a lambda expression must be declared as final or meet specific criteria to be considered effectively final。
在提供的代碼shippet中:錯誤被拋出,因為caltz變量在lambda表達式中未聲明為最終。 Java要求lambda表達式中使用的變量是最終或有效的最終來防止潛在的並發問題。
為什麼存在要求private TimeZone extractCalendarTimeZoneComponent(Calendar cal, TimeZone calTz) { // Code missing cal.getComponents().getComponents("VTIMEZONE").forEach(component -> { // Code missing if (calTz == null) { calTz = TimeZone.getTimeZone(v.getTimeZoneId().getValue()); } }); // Code missing }因此,為了確保線程安全性並防止並發問題,應將lambda表達式中使用的變量聲明為最終或有效最終,這意味著不能在lambda的範圍內修改它們。
免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。
Copyright© 2022 湘ICP备2022001581号-3