Difference between final and effectively final

后端 未结 14 3004
孤独总比滥情好
孤独总比滥情好 2020-11-22 00:38

I\'m playing with lambdas in Java 8 and I came across warning local variables referenced from a lambda expression must be final or effectively final. I know tha

14条回答
  •  日久生厌
    2020-11-22 00:59

    A variable is final or effectively final when it's initialized once and it's never mutated in its owner class. And we can't initialize it in loops or inner classes.

    Final:

    final int number;
    number = 23;
    

    Effectively Final:

    int number;
    number = 34;
    

    Note: Final and Effective Final are similar(Their value don't change after assignment) but just that effective Final variables are not declared with Keyword final.

提交回复
热议问题