Java Reference
In-Depth Information
Answer:
The answer is 5, because this refers to the enclosing Runnable, not the enclosing class
MeaningOfThis.
Verbosity in general is bad; it discourages the use of a language feature because it takes a long
time to write and maintain verbose code, and it's not pleasant to read! Good code should be easy
to comprehend at a glance. Even though anonymous classes somewhat tackle the verbosity
associated with declaring multiple concrete classes for an interface, they're still unsatisfactory.
In the context of passing a simple piece of code (for example, a boolean expression representing
a selection criterion), you still have to create an object and explicitly implement a method to
define a new behavior (for example, the method test for Predicate or the method handle for
EventHandler).
Ideally we'd like to encourage programmers to use the behavior parameterization pattern,
because as you've just seen, it makes your code more adaptive to requirement changes. In
chapter 3 you'll see that the Java 8 language designers solved this problem by introducing
lambda expressions, a more concise way to pass code. Enough suspense; here's a short preview
of how lambda expressions can help you in your quest for clean code.
Search WWH ::




Custom Search