arraylist  problem, possible pass by reference/object issue

Posted on 2007-07-24
Last Modified: 2008-02-20
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();

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.
Question by:rukiman
    LVL 92

    Expert Comment

    it should still be there, what makes u think its not?

    Author Comment

    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.
    LVL 92

    Expert Comment

    cannot see how thats happening from the code u have posted. Check your not using/checking the wrong list instance.

    Accepted Solution

    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.


    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Suggested Solutions

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now