# Help with objects

**URL:** <https://discourse.processing.org/t/help-with-objects/26590>\
**Category:** Beginners\
**Created:** [December 29, 2020, 5:35pm UTC](https://discourse.processing.org/t/help-with-objects/26590 "2020-12-29T17:35:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 29, 2020, 5:35pm UTC](https://discourse.processing.org/t/help-with-objects/26590/1 "2020-12-29T17:35:31Z")

</div>

Hi!  
I always had the issue with objects. I want to replicate  
`PVector a = new PVector(x,y,z)`  
with the class Circles. So something like

```auto
ArrayList<Circles> circle = new ArrayList<Circles>()
circle.add(new Circles( x, y, id) );

```

```auto
int n = 0;
ArrayList<Circles> circle = new ArrayList<Circles>();
void setup() {
  size(600,600);
}

void draw() {
  
}

void addNew() {
  //circle.add(new Circles(random(width),random(height),(float)0,circle.size());
}

class Circles {
  float x, y, r;
  int id;
  boolean canGrow = true;

  
  void Circles(float x_, float y_, float r_, int id_) {
    x = x_;
    y = y_;
    r = r_;
    id = id_;
  }
  void grow() {
    if(canGrow) {
      
      for(int i = 0; i < circle.size(); i++) {
        float dst = dist(x,y,circle.get(i).x,circle.get(i).y);
        if(dst != 0 && dst < r + circle.get(i).r) {
          canGrow = false;
          circle.get(i).canGrow = false;
        } else {
          r++;
        }
      }
      
    }
  }
  void display() {
    noFill();
    stroke(255);
    circle(x,y,r*2);
  }
}

```

I know there is a way to do so, but I forgot how : P

---

<div class="post-metadata">

**Author:** ![micycle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micycle/32/201_2.png) [@micycle](https://discourse.processing.org/u/micycle)\
**Post date:** [December 29, 2020, 5:54pm UTC](https://discourse.processing.org/t/help-with-objects/26590/2 "2020-12-29T17:54:51Z")

</div>

Simply remove `void` from `void Circles` to turn it from a method to a constructor.

```auto
  Circles(float x_, float y_, float r_, int id_) {
    x = x_;
    y = y_;
    r = r_;
    id = id_;
  }

```

Consider renaming the class to `Circle` and the array that contains them to `circles`.

Also, including trailing underscores for the constructor arguments (to denote internal variables) isn’t something that’s usually done in Java, since we declare variable scope explicitly!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 29, 2020, 6:02pm UTC](https://discourse.processing.org/t/help-with-objects/26590/3 "2020-12-29T18:02:02Z")

</div>

Very good answer

tutorial: [https://www.processing.org/tutorials/objects/](https://www.processing.org/tutorials/objects/)

```auto

int n = 0;
ArrayList<Circle> circles = new ArrayList<Circle>();

void setup() {
  size(600, 600);

  addNew();
  addNew();
  addNew();
  addNew();
}//func 

void draw() {

  for (Circle currentCircle : circles) {
    currentCircle.grow();
    currentCircle.display();
  }//for
}//func 

// -----------------------------------------------------------------------------------------------

void addNew() {
  circles.add(new Circle(random(30, width-30), random(30, height-30), 
    (float)0, 
    circles.size() ));
}

// ==============================================================================================

class Circle {

  float x, y, r;
  int id;
  boolean canGrow = true;

  Circle(float x_, float y_, 
    float r_, 
    int id_) {
    x = x_;
    y = y_;
    r = r_;
    id = id_;
  }
  void grow() {
    if (canGrow) {
      for (int i = 0; i < circles.size(); i++) {
        float dst = dist(x, y, circles.get(i).x, circles.get(i).y);
        if (dst != 0 && dst < r + circles.get(i).r) {
          canGrow = false;
          circles.get(i).canGrow = false;
        } else {
          r++;
        }
      }
    }
  }

  void display() {
    noFill();
    stroke(255);
    ellipse(x, y, 
      r*2, r*2);
  }
  //
}//class 
// 

```
