0

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.

Lakshman Pilaka
  • 1,803
  • 2
  • 24
  • 48

4 Answers4

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

These need to be instance variables, not static variables.

Remove the static keyword and it should behave as you want.

In laymans terms, what does 'static' mean in Java?

Community
  • 1
  • 1
user1717259
  • 2,717
  • 6
  • 30
  • 44
1

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.

Community
  • 1
  • 1
Bill
  • 4,506
  • 2
  • 18
  • 29
1

The attributes of your DataModelCollection class are static! That means they are common to all instances of the class. Thus your loop simply change the value of those static variables each time as they are common to all instances.

Tristan O.
  • 51
  • 7
1

Let me teach you some magic !

Remove the keyword static in the variables that you have declared in the class DataModelCollection and your code works fine.