Lakshman Pilaka Lakshman Pilaka - 1 year ago 59
Java Question

java array loop issue

i have a peculiar problem where while assigning elements in the loop in an array things happen okay.

when i move out of the loop and check, the same array values of all indexes will be set to those of the last index. the following is the code

JSONObject js = new JSONObject(json_string);
JSONArray jsonArray=js.getJSONArray("customer");
DataModelCollection[] dataModelCollection = new DataModelCollection[7];

for (int i=0;i<jsonArray.length();i++) {

JSONObject json = jsonArray.getJSONObject(i);

amont = json.getLong("BalanceAmount");
custName = json.getString("CustName");
partitionKey = json.getInt("PartitionKey");
String date1 = json.getString("date");

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = df.parse(date1);

dataModelCollection[i] = new DataModelCollection();


Log.i("inside_loop", ""+dataModelCollection[i].getName());


Log.i("outside_loop", ""+dataModelCollection[0].getName());
Log.i("outside_loop", ""+dataModelCollection[1].getName());
Log.i("outside_loop", ""+dataModelCollection[2].getName());
Log.i("outside_loop", ""+dataModelCollection[3].getName());
Log.i("outside_loop", ""+dataModelCollection[4].getName());
Log.i("outside_loop", ""+dataModelCollection[5].getName());
Log.i("outside_loop", ""+dataModelCollection[6].getName());

when i print the
which is basically in the loop values are shown properly. the moment execution comes out and check the "outside_loop" the values of all indices becomes dataModelCollection[6].getName() - that is the value of the last index. same is the case with

not sure where i got wrong.

for whatever its worth here's the
modal class:

public class DataModelCollection {

public static long accountNo;
public static String name;
public static double amount;
public static Date collectedDate;

public DataModelCollection(long accountNo, String name, double amount, Date collectedDate) {
this.accountNo = accountNo; = name;
this.amount = amount;
this.collectedDate = collectedDate;

public DataModelCollection() {


public String getName() {
return name;

public void setName(String name) { = name;

public double getAmount() {
return amount;

public void setAmount(double amount) {
this.amount = amount;

public Date getCollectedDate() {
return collectedDate;

public void setCollectedDate(Date collectedDate) {
this.collectedDate = collectedDate;

public long getAccountNo() {
return accountNo;

public void setAccountNo(long accountNo) {
this.accountNo = accountNo;

i am genuinely clueless of what is causing this situation. please help.

Answer Source

You'll have to remove the keyword static from each of these. Might as well change it from public to private as well since you are using setters and getters to change those values.

public static long accountNo;
public static String name;
public static double amount;
public static Date collectedDate;


private long accountNo;
private String name;
private double amount;
private Date collectedDate;

The reason they all become the last set value is because static means that variable belongs to the class and not the instance "object". So even though you are creating new objects with dataModelCollection[i] = new DataModelCollection();, when you change the static variables, they will affect all DataModelCollection objects. More information about static/instance here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download