kimmgamjja
[Java] Local variable 변수명 defined in an enclosing scope must be final or effectively final / 자바 람다식 에러 본문
[Java] Local variable 변수명 defined in an enclosing scope must be final or effectively final / 자바 람다식 에러
인절미댕댕이 2025. 3. 27. 15:19작업하다가 이 부분에서 에러 발생
list.removeIf(entry -> aId.equals(entry.get("A_ID").toString()));
에러내용은
Local variable 변수명 defined in an enclosing scope must be final or effectively final
찾아보니 람다식과 관련이 있는 에러였다
자바 컴파일러가 파라미터로 사용하는 변수와 로컬 변수를 구분하지 못해서 발생하는 에러라고 한다
로컬 변수는 람다식 안에서 바꿀 수 없기 때문에
해당 로컬 변수를 class 단에서 선언을 한 뒤 사용하면 된다
@RequestMapping("/test")
public class HomeController {
String aId = "";
public @ResponseBody Map<String, Object> test() {
list.removeIf(entry -> aId.equals(entry.get("A_ID").toString()));
}
}
* removeIf() 내용 ↓
https://kimmgamjja.tistory.com/61
[Java] removeIf() / ArrayList 삭제
removeIf()- ArrayList 의 메소드로, 인자로 전달된 조건을 충족하는 list 의 원소들을 삭제- 자바 8부터 가능- 람다식 이용하여 삭제public boolean removeIf(Predicate filter) - Predicate : 인자, 람다 표현식으로 전
kimmgamjja.tistory.com
https://wakestand.tistory.com/432
자바 Local variable '변수명' defined in an enclosing scope must be final or effectively final 에러 해결방법
해당 에러는 람다(Lambda)식을 사용할 때 발생하는 에러인데 String 배열 arr을 stream 형태로 변환한 뒤 forEach를 돌리면서 String s에 ABC를 넣어주려고 하는 코드인데 보면 Local variable '변수명' defined in an
wakestand.tistory.com
'공부 > Java' 카테고리의 다른 글
[Java] removeIf() / ArrayList 삭제 (0) | 2025.03.27 |
---|---|
STS/Eclipse 디버그 에러 Unable to install breakpoint in~ Absent Line Number Information (0) | 2025.03.26 |
STS/Eclipse Address already in use: bind 해결방법 (0) | 2025.03.13 |
[Java] Spring 에서 DB 연결 (2) | 2025.02.28 |
[Java] 2개 이상의 DB 연동 및 접속 (0) | 2025.02.28 |