I'm adding items to a List but the previous item always gets overwritten by later after the iteration.
List<CartPojo> cartLists = new ArrayList<CartPojo>();
CartPojo cartItem = new CartPojo();
for(int k=0; k<2; k++){
cartItem.setItemName("name_"+k);
cartLists.add(cartItem);
}
model.addAttribute("cartLists", cartLists);
This gives the output: name_1, name_1
Expected output: name_0, name_1