I thought anonymous classes were basically like lambdas but with worse syntax... this turns out to be true but the syntax is even worse and causes (what should be) local variables to bleed out into the containing class.

You can access none final variables by making them into fields of the parent class.

Eg

Interface:

public interface TextProcessor { public String Process(String text); }

class:

private String _key; public String toJson() { TextProcessor textProcessor = new TextProcessor() { @Override public String Process(String text) { return _key + ":" + text; } }; JSONTypeProcessor typeProcessor = new JSONTypeProcessor(textProcessor); foreach(String key : keys) { _key = key; typeProcessor.doStuffThatUsesLambda(); }

I dont know if they've sorted this out in java 8 (I'm stuck in EE world and not got 8 yet) but in C# it would look like this:

public string ToJson() { string key = null; var typeProcessor = new JSONTypeProcessor(text => key + ":" + text); foreach (var theKey in keys) { key = theKey; typeProcessor.doStuffThatUsesLambda(); } }

You dont need a seperate interface in c# either... I miss it! I find myself making worse designs in java and repeating myself more because the amount of code + complexity you have to add in java to reuse something is worse than just copy and pasting a lot of the time.