• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 862
  • Last Modified:

arraylist problem, possible pass by reference/object issue

public class Bags implements Serializable {
    private ArrayList<BagItem> list;

    public Bags() {
        list = new ArrayList<BagItem>();
    }

    public ArrayList<BagItem> getList() {
        return list;
    }

    public void setList(ArrayList<BagItem> list) {
        this.list= list;
    }
}

public class BagsEJB3Bean implements BagsInf {

    public void loadData(Bags data) throws ProcessingException {
        BagItem item1= new Bagitem();
        item1.setName("test");
        data.getList().add(item1);
    }
}

public class test
{
    private Bags bags;
    public test()
    {
         bags = new Bags();
         BagsInf inf = EJBHelper.GetBagsEJB3(); <-- to get interface to EJBean
         inf.loadData(bags); <-- load some data, item added within this function, but once returns its not there
    }
}

Why is it that in loadData, I can see that the item has been added, but when the function returns back to where is was called from constructor test(), the item added is no longer there.
0
rukiman
Asked:
rukiman
  • 2
1 Solution
 
objectsCommented:
it should still be there, what makes u think its not?
0
 
rukimanAuthor Commented:
because this is a snippet of code for a JSF page and it doesn't appear there, and I have stepped through the program and I can see that when I step into loadData() the list is empty then size becomes one, and when I come out of loadData(), the list size is 0 as opposed to 1 which is what I expect.
0
 
objectsCommented:
cannot see how thats happening from the code u have posted. Check your not using/checking the wrong list instance.
0
 
guioguioCommented:
When you call  "inf.loadData(bags);"
the reference passed to the method is a copy of the original reference.
that's why it can't modify the original.

maybe you could just get the new "bag" through the return value.

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now