0

In my program I assign objects from one class to objects of the same type in another class ,that worked for me except with bigdecimal here is a code snippet

 public class MasterDetail extends JFrame {

 private ArrayList<SellBean> sellRecords;
 private JLabel totlLbl;
 private BigDecimal totalProfit;

in the button action i call the constructor of the other class and pass these objects to it

 previewBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if (!(sellRecords.isEmpty())) {
                new Preview(sellRecords, totlLbl,totalProfit);
            }

in the other class i create a corresponding object to assign to the received ones

 public class Preview extends JFrame {

   private ArrayList<SellBean> sellRecords;
   private JLabel totlLbl;
   private BigDecimal totalProfit;

 public Preview(ArrayList<SellBean> sellRecords, JLabel totlLbl,
        BigDecimal totalProfit) {
    this.sellRecords = sellRecords;
    this.totlLbl = totlLbl;
    this.totalProfit = totalProfit;
    }
    }

the problem is when i make any change to the sellRecords or totlLbl of the Preview class that change reflects to the other class MasterDetail but when i change the bigdecimal object totalProfit of Preview it changes in Preview but not reflects the other class as the previous objects . here is the code where i try to change values in Preview class and want to reflect MasterDetail class

    btnNewButton = new JButton("delete item");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if (table.getSelectedRow() >= 0) {

                SellBean bean = Preview.this.sellRecords.get(table
                        .getSelectedRow());

                Preview.this.totlLbl.setText(Double
                        .valueOf(Preview.this.totlLbl.getText())
                        - (Double.valueOf(bean.getSELL_PRICE().toString()) * Double
                                .valueOf(bean.getQUANTITY().toString()))
                        + "");
                BigDecimal profit = (new BigDecimal(bean.getSELL_PRICE()
                        .toString()).subtract(new BigDecimal(bean
                        .getBUY_PRICE().toString())))
                        .multiply(new BigDecimal(bean.getQUANTITY()
                                .toString()));
                Preview.this.totalProfit = Preview.this.totalProfit.subtract(profit);


                Preview.this.sellRecords.remove(table.getSelectedRow());
                }

this worked for "totlLbl" and "sellRecords" but not for "totalProfit" . Can anyone help me?

Al Lelopath
  • 6,141
  • 13
  • 72
  • 132
  • possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Radiodef Nov 17 '14 at 23:43
  • *"in the other class i create a corresponding objects"* You are not creating any objects there. You are passing *references* to the constructor. That is what you need to understand. When you *reassign a reference* (like you are doing with BigDecimal) that is a wholly different thing from *mutating an object's internal state* with a method call (like you do with the JLabel and ArrayList). – Radiodef Nov 17 '14 at 23:47
  • To continue from Radiodef's comment, this is how you create an object and refer to its instance variable: `Preview myPreview = new Preview(constructor parameters); myPreview.totalProfit = myPreview.totalProfit.subtract(profit);` – Perry Monschau Nov 17 '14 at 23:52
  • so what is the solution to my target? – mohamed_elsharkawey Nov 18 '14 at 10:09

0 Answers0