Is it good to create a new object at each iteration? One of my friends told me that it is not a good practice to create such cases.
If you need different separate instances, you need to create them. Since your list requires five objects with different names each (ram, mohan, anil, etc.), you need a new object for each iteration of the loop. How else are you going to store five names?
Regarding declaring the variable a outside the loop, I don’t think it makes a difference in performance, but also reduces readability.
A a;
You might be interested in the for-each loop
for(String name: test){ A a = new A(); a.setName(name); list.add(a); }
source share