S
Stefan Ram
What allows closures in the JLS? By "closures" I mean cases
like the following, where the parameter »i« is enclosed in
objects c0 and c1:
Main.java
public final class Main
{
public static java.lang.Runnable f( final int i )
{ return new java.lang.Runnable()
{ public void run()
{ java.lang.System.out.println( i ); }}; }
public static void main( final java.lang.String[] args )
{ final java.lang.Runnable c0 = f( 0 );
final java.lang.Runnable c1 = f( 1 );
c0.run();
c1.run(); }}
java.lang.System.out
0
1
The JLS, in fact contains the following word sequence:
"local variables from the surrounding method may be
referred to"
- JLS7, 8.1.3, Example 8.1.3-2.
and this is just what I was looking for. Why do I still post
this question here? Because this sentence "only" appears in
an example (Example 8.1.3-2). I am looking for the same
contents in a more normative part.
It would be nice if the JLS7 would contain some normative
part that allows this and also gives semantics for it (to
the extend that such local variables will keep their value
even when the lifetime of their method incarnation already
has ended). Also, it should be said somewhere that
parameters are also local variables in this sense. Possibly,
the JLS7 contains such wording and I just cannot find it.
like the following, where the parameter »i« is enclosed in
objects c0 and c1:
Main.java
public final class Main
{
public static java.lang.Runnable f( final int i )
{ return new java.lang.Runnable()
{ public void run()
{ java.lang.System.out.println( i ); }}; }
public static void main( final java.lang.String[] args )
{ final java.lang.Runnable c0 = f( 0 );
final java.lang.Runnable c1 = f( 1 );
c0.run();
c1.run(); }}
java.lang.System.out
0
1
The JLS, in fact contains the following word sequence:
"local variables from the surrounding method may be
referred to"
- JLS7, 8.1.3, Example 8.1.3-2.
and this is just what I was looking for. Why do I still post
this question here? Because this sentence "only" appears in
an example (Example 8.1.3-2). I am looking for the same
contents in a more normative part.
It would be nice if the JLS7 would contain some normative
part that allows this and also gives semantics for it (to
the extend that such local variables will keep their value
even when the lifetime of their method incarnation already
has ended). Also, it should be said somewhere that
parameters are also local variables in this sense. Possibly,
the JLS7 contains such wording and I just cannot find it.