1

I'm currently working with code that has multiple instances of a particular class. Each instance runs through certain tasks, and collects data that is sent to a display layer for display. The problem that is occurring is that when a new instance of the class is created, it resets all other instances of that class that currently exist. Here's an example that will hopefully illustrate my point

Foo instanceFooOne = fooService.performActions(data); 
Foo instanceFooTwo = fooService.performActions(data); 

class FooService {
    public void performActions(Object data) {
        Foo foo = new Foo();
        return foo;
    }
}

To me, this should have two separate instances of the Foo object, but instead has only the latest version of Foo for all instance of the Foo object created. Is there any way to not have this happen?

canadiancreed
  • 1,966
  • 6
  • 41
  • 58
  • Is it possible that fields in `Foo` are `static`? – Pshemo Oct 07 '14 at 18:23
  • 3
    I'm going to bet that you that you don't make a new copy of `data` in the `Foo` constructor. This means that both of the `Foo` objects you are making are storing a reference to the same `data`. If you change `data`, all `Foo`s will see the change. – azurefrog Oct 07 '14 at 18:23
  • The `foo` instance is different, but the stored `data` object is not. – EpicPandaForce Oct 07 '14 at 18:25
  • 1
    We can't tell without seeing the source of `Foo` but my money is on @azurefrog. – biziclop Oct 07 '14 at 18:27
  • @Pshemo Nope no static items to be found. – canadiancreed Oct 07 '14 at 18:39
  • Then most probable cause of your problem is as azurefrog explained, because in both `Foo` you are using exact same `data` instance. To confirm it we would need to see how your `Foo` class code looks like. – Pshemo Oct 07 '14 at 18:43
  • @azurefrog So by passing the data Object we're in fact passing a reference to where it's stored in memory? It would at least explain why it's being reset. I'll try creating a copy of data within the constructor and see what happens. – canadiancreed Oct 07 '14 at 18:44
  • Yes, Java is pass-by-value, with `performActions(data)` you are passing value of `data` reference which is memory address of instance which it holds. – Pshemo Oct 07 '14 at 18:45
  • @canadiancreed Yup. See [this SO article](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) or also [this blog post](http://javadude.com/articles/passbyvalue.htm) for a more complete explanation/discussion. – azurefrog Oct 07 '14 at 18:48

1 Answers1

1

You need to save the reference of data. Your currently only referencing memory for "data" so all other instances will change.

Foo instanceFooOne = fooService.performActions(data); 
Foo instanceFooTwo = fooService.performActions(data); 

class Foo{
private Object data;
    public Foo(Object data) {
        this.data = data;
    }
public Object getData(){
  return data;
}
}

Then use:

instanceFooOne.getData();

To quote Gevorg:

Java is pass-by-value because inside a method you can modify the referenced Object as much as you want but no matter how hard you try you'll never be able to modify the passed variable that will keep referencing the same Object no matter what!

Petro
  • 3,484
  • 3
  • 32
  • 59