Content deleted Content added
Line 111:
Java has no first class functions, therefore has no closures.
Actually that java example on the anonymous class is overcomplicated for its result. The following code performs the same function without the need of creating an embedded runnable class. This is because the embedded Runnable classes run method is just called from the Thread classes run. Anyone who does threading in java knows this, which is why we can just extend thread instead of implementing Runnable. In such a case as this, it would be better formed code to just extend the thread into an anonymous, instead of creating an embedded runnable.
<source lang="java">
class CalculationWindow extends JFrame {
private volatile int result;
...
public void calculateInSeparateThread(final URI uri) {
// The expression "new Thread() { ... }" is an anonymous class.
new Thread() {
public void run() {
// It can read final local variables:
calculate(uri);
// It can access private fields of the enclosing class:
result = result + 10;
}
}.start();
}
}
</source>
[[Special:Contributions/76.235.207.239|76.235.207.239]] ([[User talk:76.235.207.239|talk]]) 09:11, 29 May 2011 (UTC)
==Closures and Python==
|