Skip to content

람다와 관련된 코드 스니펫 해결하기 #28

Discussion options

You must be logged in to vote
  1. p113 의 내용을 참조 하면, 지역 변수의 제약을 볼 수 있음.
  • 지역 변수는 명시적으로 final로 선언되어 있어야 하거나 실질적으로 final로 선언된 변수와 똑같이 사용 되어야 함.

따라서 오류의 원인은 stock 변수에 값이 두 번 할당 된것이 원인임.


  1. 람다식에서 사용하고 있는 값을 복사해서 사용할 수 있음.
        final int currentStock = stock;
        Runnable r = () -> {
            System.out.println("현재 재고: " + currentStock);
        };

이와 같은 이유는 바로 불변성을 지키기 위함이라 생각함.

참조 해보면 좋은 글 - https://github.com/orgs/Study-2-Effective-Java/discussions/37

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by corock
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants