S
Steve Brecher
class Foo<Integer> implements Collection<Integer> {...}
class Bar {
Foo<Integer> aFoo() {
return new Foo<Integer>(...);
}
void method() {
for (Integer i : aFoo())
...;
}
During the loop execution method will not have an instance of Foo on its
stack, but only an instance of Iterator<Integer>, and thus I think that
there is no instance of Foo that a garbage collector can see as live, and
hence the instance created by aFoo is subject to destruction during the
loop. Therefore it is necessary to write instead something like,
myFoo = aFoo();
for (Integer i : myFoo)
...;
Right?
class Bar {
Foo<Integer> aFoo() {
return new Foo<Integer>(...);
}
void method() {
for (Integer i : aFoo())
...;
}
During the loop execution method will not have an instance of Foo on its
stack, but only an instance of Iterator<Integer>, and thus I think that
there is no instance of Foo that a garbage collector can see as live, and
hence the instance created by aFoo is subject to destruction during the
loop. Therefore it is necessary to write instead something like,
myFoo = aFoo();
for (Integer i : myFoo)
...;
Right?