Re: "final" in "for"
Daniel Pitts wrote:
Roedy Green wrote:
On Mon, 14 Dec 2009 17:58:30 -0800, Daniel Pitts
<newsgroup.spamfilter@virtualinfinity.net> wrote, quoted or indirectly
quoted someone who said :
for (String s: someSet) {
s = "nope";
}
compiles and runs.
Arrgh. I guess then you could extend java with
for ( final String s : someSet )
I sure hope lints complain at s="nope";
That is an existing construct, which does what you expect :-) No need to
extend.
SSCCE (JDK 1.6.0_17):
package testit;
public class Freach
{
public static void main( String [] args )
{
Set <String> strangs = new HashSet <String> ();
Set <Integer> ants = new HashSet <Integer> ();
for ( String strang : strangs )
{
System.out.println( "strang = \""+ strang +"\"" );
strang = "else";
}
for ( final String strang : strangs )
{
System.out.println( "strang = \""+ strang +"\"" );
// strang = "else"; // compiler error
}
for ( int ant : ants )
{
System.out.println( "ant = \""+ ant +"\"" );
ant = 17;
}
for ( final int ant : ants )
{
System.out.println( "ant = \""+ ant +"\"" );
// ant = 17; // compiler error
}
}
}
--
Lew