It is my understanding that each time I instantiate friend, I reuse the same location in memory, overwriting the existing Person object, if present.
No, that's not the case. You're overwriting the previous value in the variable - but that's not the object. That's just a reference to the object. There's another reference to the object within the list. The object itself won't be overwritten at all. Each object you create is independent of the others.
In terms of the difference between declaring the variable inside the loop or outside the loop, it doesn't make much difference. There are a few differences:
- If it's declared outside the loop, you can use the variable within the loop before assigning a new value to it, so you get to see the previous value
- If it's declared outside the loop, you can use the variable after the loop as well, to read the last-assigned value
- If it's declared outside the loop, and you use an anonymous function inside the loop, then every anonymous function will capture the same variable. If it's inside the loop, each anonymous function will capture a different variable. If all of this sounds like gobbledygook to you at the moment, you can probably ignore it
- If it's declared inside the loop, then in a separate block you can declare a separate variable with the same name; you can't do that if it's declared outside the loop.
I'd generally recommend declaring variables with the minimum scope possible, at the point where you first need it - I find that ends up with clearer code.