# Questions about BouncyBubbles example

**URL:** <https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883>\
**Category:** Coding Questions\
**Created:** [November 24, 2018, 12:56pm UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883 "2018-11-24T12:56:30Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![amundsen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/amundsen/32/2497_2.png) [@amundsen](https://discourse.processing.org/u/amundsen)\
**Post date:** [November 25, 2018, 10:45am UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/21 "2018-11-25T10:45:21Z")

</div>

Just to be sure. The part below is called a _constructor_, right?

```auto
  Ball(float xin, float yin, float din, int idin, Ball[] oin) {
    x = xin;
    y = yin;
    diameter = din;
    id = idin;
    others = oin;
  }

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 25, 2018, 10:49am UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/22 "2018-11-25T10:49:54Z")

</div>

> [@amundsen](#):
>
> _constructor_

> **[Java Articles - dummies](https://www.dummies.com/category/articles/java-33602/)**
>
> Java powers much of the online world and tons of apps and programs. You can read all about it right here. (Perhaps over a cup of java?)

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [November 25, 2018, 10:51am UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/23 "2018-11-25T10:51:58Z")

</div>

Okay, so sorry about earlier but I made a mistake. I forgot that when you assign an array like this you are not creating a real copy of the object so the behavior is a bit different.

When you do this: `others = oin` you are not create a copy of `oin` (wich is you `balls` array). What you are doing is giving another name to your `balls` array, another way to access it.

This way, no matter the changes that you do on `balls` you will also have it on `others` since it is actually the same thing but just called differently, a bit like a nickname.

Try out the simplify code bellow to understand it.

```auto
final int numBalls = 10;
Ball[] balls = new Ball[numBalls];

void setup() {
  // Create the balls
  for (int i = 0; i < numBalls; i++) {
    balls[i] = new Ball("Ball_" + i, balls);
  }

  // Describe the balls
  for (int i = 0; i < numBalls; i++) {
    balls[i].describeItself();
  }
}

class Ball {
  private String name;
  private Ball[] others;

  // Constructor
  Ball(String name, Ball[] oin) {
    this.name = name;
    this.others = oin;
  }

  // Description of the ball - list all the elements of others
  void describeItself() {
    println("Name: " + name);
    printArray(others);
    //for (int i = 0; i < others.length; i++) {
    // println("[" + i + "]" + " " + others[i].getName());
    //}
    println("");
  }
  
  // Return the name of the ball
  String getName() {
    return name;
  }
}

```

So in the end, all the ball that you have created will have a way to interact with all the other balls that you have created. Another way to do it would be to first create all your balls and then update your others array. The following code does exactly the same but is easier to understand:

```auto
final int numBalls = 10;
Ball[] balls = new Ball[numBalls];

void setup() {
  // First you create all the balls
  for (int i = 0; i < numBalls; i++) {
    balls[i] = new Ball("Ball_" + i);
  }

  // Then you update the others array of all your balls
  for (int i = 0; i < numBalls; i++) {
    balls[i].updateOthers(balls);
  }
  
  // Describe the balls
  for (int i = 0; i < numBalls; i++) {
    balls[i].describeItself();
  }
}

class Ball {
  private String name;
  private Ball[] others;

  // Constructor
  Ball(String name) {
    this.name = name;
  }

  void updateOthers(Ball[] oin) {
    others = oin;
  }
  
  // Description of the ball - list all the elements of others
  void describeItself() {
    println("Name: " + name);
    printArray(others);
    //for (int i = 0; i < others.length; i++) {
    // println("[" + i + "]" + " " + others[i].getName());
    //}
    println("");
  }
  
  // Return the name of the ball
  String getName() {
    return name;
  }
}

```

---

<div class="post-metadata">

**Author:** ![amundsen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/amundsen/32/2497_2.png) [@amundsen](https://discourse.processing.org/u/amundsen)\
**Post date:** [November 25, 2018, 11:01am UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/24 "2018-11-25T11:01:24Z")

</div>

> [@jb4x](#):
>
> When you do this: `others = oin` you are not create a copy of `oin` (wich is you `balls` array). What you are doing is giving another name to your `balls` array, another way to access it.

OK. Two consequent questions:

1. It this possibility of having two ways to access the same thing specific to Java or does it exist in other languages?
2. Doesn’t this way of coding spoil memory as each ball has an unuseful reference to itself within the “others” array?

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [November 25, 2018, 11:04am UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/25 "2018-11-25T11:04:30Z")

</div>

1. It is not specific to java. You can read about references and pointers.
2. Since you are just referring to something and not creating another copy of it you are not spoiling memories.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 25, 2018, 11:57am UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/26 "2018-11-25T11:57:05Z")

</div>

> [@amundsen](#):
>
> Doesn’t this way of coding spoil memory as each ball has an unuseful reference to itself within the “others” array?

If by that you mean having extra alias variables for the same object, worry not. 👼  
Each reference variable in Java just uses up 4 bytes (8 bytes in rare cases). It’s pretty insignificant. 💁‍♂️

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 25, 2018, 12:02pm UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/27 "2018-11-25T12:02:07Z")

</div>

Not exactly balls bouncing off each other, but merely a sketch where a **line()** is drawn linking them: 🏐  
[Studio.ProcessingTogether.com/sp/pad/export/ro.989GaZC5t7EkE](http://Studio.ProcessingTogether.com/sp/pad/export/ro.989GaZC5t7EkE)

Notice though on the sketch above, the Bubble class doesn’t access the global field _balls[]_. ✋  
The whole job is done within **draw()**, which is solely responsible to iterate over the array _balls[]_. 🤓

[Previous page](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883.md?page=1)
