0 Replies Latest reply on Feb 4, 2002 3:00 PM by mpetteys

    Pass-by-Ref vs Pass-by-Value in CMP

    mpetteys


      I just stumbled upon something confusing behavior in a CMP EJB. Is there a reason that I am not aware of that the following code segment does not work in a CMP bean but the code segment at the bottom should. I have a field in my BMP bean called ListData. It works when I use setListData but when I try to use the addToList function below it doesn't work. Is this a bug or is it not supposed to work?

      This does not work. Every time addToList is executed it starts off with the ListData object that is persisted. setListData does modify the data correctly though.

      public abstract class DataBean implements javax.ejb.EntityBean {

      ...

      abstract public List getListData();
      abstract voic setListData(List l);

      public void addToList(String s) {
      List l = this.getReferenceData();
      l.add(s);
      this.setListData(l);
      }
      }

      but this does.. NOTE that I create a new ArrayList instead of using the List that is returned by this.getReferenceData();

      public abstract class DataBean implements javax.ejb.EntityBean {

      ...

      abstract public List getListData();
      abstract voic setListData(List l);
      public void addToList(String s) {
      ArrayList l = new ArrayList(this.getReferenceData());
      l.add(s);
      this.setListData(l);
      }

      }