[Oberon] Life without local procedures
Claudio Nieder
private at claudio.ch
Sat Feb 24 14:02:57 CET 2018
Hi,
> BTW, I finally found a solution for local methods in Java:
>
> While it is not possible to declare methods within methods, it is
> actually possible to declare local classes within methods. Therefore, I
> declared a local class with my "local" methods and then created a single
> instance of the local class to invoke them. Since methods within the
> local class can also access variables declared in the surrounding
> method, this allows for access to "intermediate" variables.
>
> While this seems weird at first sight, it is actually quite logical: in
> Java every method belongs to a class; thus, one can not just add new
> methods within a limited scope, but needs a separate class to collect them.
Maybe nicer using functions:
package test;
import java.util.function.Function;
public class Global
{
public static void main(final String[] a)
{
final int i=5;
final Function<Integer,Integer> q=x -> {
return x*i;
};
System.out.println(q.apply(7));
}
}
claudio
--
Claudio Nieder, Ruhestrasse 7, CH-8045 Zürich, Tel +4179 357 6743, www.claudio.ch
More information about the Oberon
mailing list