0

I am pretty new to this language. I have an object and I want to pass it by value to another method:

// Drawings
class Drawings
{
   // Has some variables

   Drawings()
   {

   }

   // Hass some methods

} 

class History
{
    ArrayList<Drawings> prevDrawings;

    History()
    { 
        this.prevDrawings = new ArrayList<Drawings>();
    }

    void add(Drawings newDrawing) {}

}

Now, say I have a Drawings myDrawing and History myHistory, and I want to pass myDrawing by value to myHistory. The following way passes it by reference:

myHistory.add(myDrawing);

How can I then pass this by value?

BitNinja
  • 1,477
  • 1
  • 19
  • 25
Kousha
  • 32,871
  • 51
  • 172
  • 296
  • What you mean by passing an instance with pass by value? Did read this http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Thusitha Thilina Dayaratne Aug 06 '14 at 05:33
  • I mean that by default, the object is passed by reference. So if I modify the object anywhere, all instances of it are modified. I don't want that. I want to send a copy only. – Kousha Aug 06 '14 at 06:10
  • Can't you get a clone of the object inside the method and manipulate cloned object? Will it solve your problem? – Thusitha Thilina Dayaratne Aug 06 '14 at 08:40

1 Answers1

0

Everything in Java, and therefore Processing, is already pass-by-value. Nothing is passed by reference.

If that doesn't make sense, read this and then read this.

What you might mean is that you want to pass a copy of the Object into a method. There are multiple ways to do that. Let's say you have this as your Object:

class MyObject{
   String thing1;
   double thing2;
   int thing3;
}

You could create a copy method outside of the MyObject class:

public MyObject copy(MyObject original){
   MyObject copy = new MyObject();
   copy.thing1 = original.thing1;
   copy.thing2 = original.thing2;
   copy.thing3 = original.thing3;
}

You could create a copy method inside the MyObject class:

class MyObject{
   String thing1;
   double thing2;
   int thing3;

   public MyObject copy(){
      MyObject copy = new MyObject();
      copy.thing1 = this.thing1;
      copy.thing2 = this.thing2;
      copy.thing3 = this.thing3;
   }

}

Or you could create a copy constructor:

class MyObject{
   String thing1;
   double thing2;
   int thing3;

   public MyObject(MyObject copy){
      this.thing1 = copy.thing1;
      this.thing2 = copy.thing2;
      this.thing3 = copy.thing3;
   }
}

You could also use serialization, but that goes a little outside normal Processing.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • On the website, http://www.processing.org/tutorials/objects/, it is said that if an object is passed to a function, then a copy is not made, and therefore any changes made anywhere to that object is reflected everywhere. That is, passed by reference, isn't it? – Kousha Aug 07 '14 at 00:49
  • No. Please read the links I posted above. To demonstrate that it's not passing by reference, run the following (unformattable) code: void setup(){String x = "one"; myFunction(x); println(x);} void myFunction(String x){x = "two";} --- if it was passing by reference, this would print out "two", but it doesn't. – Kevin Workman Aug 07 '14 at 12:43
  • Thanks for the info. But 2 things: again on that site it says that a primitive (like float string) is passed by copy, but an object is not! It's right there on the site (I am not making this up or imagining it). In my example in the question, when I pass the object `myDrawings` to `History`, they both become the same object. If `myDrawings` is modified in `setup()`, its value is also modified in `History` – Kousha Aug 07 '14 at 17:08
  • This might be confusing because you're passing the **value** of a **reference** which is NOT the same as passing a reference, as I showed in my above example. But this is all a bit of extra info, as my answer of needing to create a copy is true, regardless of pass-by-value vs pass-by-reference. – Kevin Workman Aug 07 '14 at 17:10