I didn't expect that new
doesn't change the instance variables if instance variables are already assigned.
Did I understand correctly?
class Foo {
String name = "Java";
}
class Main {
public static void main(String args[]) {
Foo foos[] = { new Foo(), new Foo() };
foos[0].name = "bar";
for (Foo c : foos) c = new Foo();
for (Foo c : foos) System.out.println(c.name);
for (Foo c : foos) c.name = "baz";
for (Foo c : foos) System.out.println(c.name);
}
}
Output
bar
Java
baz
baz
In the above example, the new
works more like casting the object to the same class is already it. What is the practical use for this behavior?