-3

I have such piece of code:

DataTableFactory<Object> TempDataTableFactory = new DataTableFactory<Object>();
DataTable<Object> tempDataTable = TempDataTableFactory.getInstance();
tempDataTable = dataTable;
ExecutedArguments e = new ExecutedArguments(); 
e.setDataTable(tempDataTable);
e.setExecutedCommand(cmd);
stack.addNewExecutedCommand(e);
result = operation.execute();  

Now I just want to keep the olddataTable before execution. When I debug my code till the line result = operation.execute(); there is no problem. In that line I change the dataTable. But because tempDataTable points to dataTable it also changes. But I don't want tempDataTable to change. How can I do this?

Anne
  • 26,765
  • 9
  • 65
  • 71
brtb
  • 2,201
  • 6
  • 34
  • 53
  • 3
    This has ... nothing to do with pass-by-value or pass-by-reference. – Brian Roach Nov 14 '11 at 00:02
  • @Brian I'm not sure that's true. If it was pass by value, the entire object would get passed but not modified in the calling procedure, wouldn't it? – corsiKa Nov 14 '11 at 01:09
  • 1
    Java is *only* pass by value. It just so happens the value is a memory address ;) Aside from that ... I really can't make heads or tails of his question and why that has anything to do with the price of tea in China :) – Brian Roach Nov 14 '11 at 01:14
  • @Brian I know that Java is strictly pass by value; but if the object was passed by value instead of the reference, his problem would be solved without code changes. But of course, you can't, so you have to `clone` it or something similar and pass your clone in. – corsiKa Nov 14 '11 at 18:13

1 Answers1

2

If it supports cloning, I would use tempDataTable.clone(). Otherwise you'll have to implement a copy constructor.

corsiKa
  • 81,495
  • 25
  • 153
  • 204