1

I have a class Order as shown below :

public class Order {

    private List<OrderLine> orderLines = new ArrayList<OrderLine>();


    public void add(OrderLine o) throws Exception {
        if (o == null) {
            System.err.println("ERROR - Order is NULL");

        }
        orderLines.add(o);
    }
    public void clear() {
      this.orderLines.clear();

    }
}

Now in my main I want to use one variable of order and update list and print the output

My Main class code:

 public static void main(String[] args) throws Exception {

        Map<String, Order> allOrders = new HashMap<String, Order>();

        //Build Order 1
        Order order = new Order();
        order.add(new OrderLine(new Item("book", (float) 12.49), 1));
        order.add(new OrderLine(new Item("Pen", (float) 14.99), 1));

        allOrders.put("Order 1", order);
        order.clear();



        //Build Order 2

        // Reuse cart for an other order


        order.add(new OrderLine(new Item("imported box of chocolate", 10), 1));
        order.add(new OrderLine(new Item("perfume", (float) 47.50), 1));
        allOrders.put("Order 2", order);
        order.clear();
}

Here I am creating one of the order object and addign details and adding this to list.Now Clearing the data in the object and updating the object with new order details but the first order details are also updated with second order details.I think Order object is Pass by reference.How to achieve object reuse avoiding pass by reference and have all order details??

Regards, Nagasree.

3

3 Answers 3

1

If you add the same Order instance to the Map multiple times, all the values in your Map will be identical.

Instead of calling clear(), create a new Order :

replace

order.clear();

with

order = new Order ();

It would make sense to use your paradigm of reusing a single object if you didn't add that object to the Map.

For example, you can use a single StringBuilder instance to generate Strings that would be added to the Map :

Map<Integer,String> map = new HashMap<>();
StringBuilder sb = new StringBuilder(128);
sb.append("one");
map.put(1,sb.toString());

sb.setLength(0); // equivalent to your clear()
sb.append("two"); // reuse same StringBuilder instance
map.put(2,sb.toString());
3
  • Actually challenge is not to create another object of Order and achieve.I thought may be I am missing some concept of java.
    – NAGASREE
    Commented Sep 29, 2016 at 8:38
  • @NAGASREE If you must put the Order instance a value in the Map, you can't use a single Order object. If you only want to print the data represented by these orders, you can use a single Order instance, but put in the Map order.toString() (assuming your Order class overrides toString().
    – Eran
    Commented Sep 29, 2016 at 8:41
  • Thank you for the explanation.
    – NAGASREE
    Commented Sep 29, 2016 at 8:44
0
  • you create an object for your order.

  • you put this object in a list

  • you edit this same object

  • you (re) put this same object in the list

-> your list contains 2 times the same object

in fact:

updating the object with new order details

should be

updating the object with another order details

-1

You need something like Singleton

Or just do that:

order = new Order ();
1
  • singleton is exactly the opposite of you second suggestion Commented Sep 29, 2016 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.