람다와 관련된 코드 스니펫 해결하기 #28
Answered
by
Irisation23
corock
asked this question in
CHAPTER 3 람다 표현식 - Q&A
-
public class InventoryService {
public static void subtract(int stock) {
stock--;
Runnable r = () -> {
// 아래 코드에서 컴파일 오류 발생
System.out.println("현재 재고: " + stock);
};
stock *= 2;
r.run();
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
Irisation23
Jun 20, 2023
Replies: 2 comments
-
final int currentStock = stock;
Runnable r = () -> {
System.out.println("현재 재고: " + currentStock);
}; 이와 같은 이유는 바로 불변성을 지키기 위함이라 생각함.
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
corock
-
stock--;
int finalStock = stock;
Runnable r = () -> {
// 아래 코드에서 컴파일 오류 발생
System.out.println("현재 재고: " + finalStock);
};
@bunsung92 님께서 말씀 해주셨듯 또한 해당 질문의 내용이 순수 함수와도 관련이 있는 것 같아 참고 디스커션 댓글과 관련이 있지 않나 싶어 참고 링크를 남깁니다. 참고 링크 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
지역 변수의 제약
을 볼 수 있음.final
로 선언된 변수와 똑같이 사용 되어야 함.이와 같은 이유는 바로 불변성을 지키기 위함이라 생각함.