I've been studying for my OCP certification and I've come across something I don't understand.
Note: the code below is non-sense and not meant to do anything real but rather to illustrate the behavior that I'm curious about. I would never write production code like this but I can't help but be puzzled as to why it works this way.
The following code compiles w/o issue
char end = 'z'; String s = ""; Predicate<Character> predicate = c -> { char start = 'a'; return start <= c && c <= end; }; char c = 'Z'; char start = 'B';
if I move the final two lines of that block above the predicate definition it doesn't compile and it instead complains that I'm attempting to redeclare c & start inside my predicate.
char end = 'z'; String s = ""; char c = 'Z'; char start = 'B'; Predicate<Character> predicate = c -> { char start = 'a'; return start <= c && c <= end; };
I assume this is something about how Java deals with lambda methods but why does having the declarations for c & start before the predicate cause issues while having them below does not?