Lakshman Pilaka Lakshman Pilaka - 4 months ago 8
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();

dataModelCollection[i].setName(custName);
dataModelCollection[i].setAccountNo(partitionKey);
dataModelCollection[i].setAmount(amont);
dataModelCollection[i].setCollectedDate(date);


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
inside_loop
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
amount
,
date
,
partitionKey
.

not sure where i got wrong.

for whatever its worth here's the
DataModelCollection
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;
this.name = name;
this.amount = amount;
this.collectedDate = collectedDate;
}

public DataModelCollection() {

}

public String getName() {
return name;
}

public void setName(String name) {
this.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

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;

to

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.