We can't reliably answer this without knowing what temp.printInfo()
and makeUseOf()
are doing. It is easy to implement them to behave the way you describe though.
When you instantiate temp
outside the loop, you will be using the same object throughout all iterations of the loop. Thus it is possible for it to gather data from each iteration (e.g. into a collection). And then it is possible for methods to get data accumulated in the current iteration, as well as from any previous iteration, which may cause problems if it was not intended.
So let's assume temp
contains a collection and in each iteration a column from the resultset is added to it. Now if temp.printInfo()
is implemented to print info about the last element in this collection, while makeUseOf()
is implemented to do something with the first element in the collection, you get the behaviour you observed.
OTOH when you instantiate temp
inside the loop, you will get a new, distinct object in each iteration, which won't "remember" any data from earlier iterations. Thus even with the implementations of temp.printInfo()
and makeUseOf()
outlined above, you will get correct results.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…