# How to initialize CLASS by it's own function?

**URL:** <https://discourse.processing.org/t/how-to-initialize-class-by-its-own-function/23946>\
**Category:** Coding Questions\
**Created:** [September 18, 2020, 10:17pm UTC](https://discourse.processing.org/t/how-to-initialize-class-by-its-own-function/23946 "2020-09-18T22:17:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![IpNeNaEcRe](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ipnenaecre/32/11223_2.png) [@IpNeNaEcRe](https://discourse.processing.org/u/IpNeNaEcRe)\
**Post date:** [September 18, 2020, 10:17pm UTC](https://discourse.processing.org/t/how-to-initialize-class-by-its-own-function/23946/1 "2020-09-18T22:17:28Z")

</div>

I can assign a new value to the variables.  
But I can’t use constructor to initialize the variables in the object function.

```auto
int nums = 20;
Drop[] drops = new Drop[nums]; 

void setup() { 
  size(800, 800); 
  smooth();
  // initialize
  for (int i=0; i < drops.length; i++) {
    drops[i] = new Drop();
  }
} 

void draw() { 
  background(255); 

  for (int i=0; i < drops.length; i++) { 
    drops[i].display();
    drops[i].move();

    //// 1.Works well
    //if (drops[i].reachedBottom1()) {
    // drops[i] = new Drop();
    //}

    //// 2.Works well
    //drops[i].reachedBottom2();
    
    // 3.Why didn't work?
    drops[i].reachedBottom3(drops[i]);
  }
}

class Drop { 
  float x, y;
  float speed;
  float r;

  Drop() { 
    r = random(2, 6); 
    x = random(width);
    y = -r; 
    speed = r;
  } 

  void move() { 
    y += speed;
  } 

  void display() { 
    fill(50, 100, 150); 
    noStroke(); 
    circle(x, y, 2*r);
  }

  // 1.Works well
  boolean reachedBottom1() {
    if (y > height+r) { 
      return true;
    } else {
      return false;
    }
  }

  // 2.Works well
  void reachedBottom2() {
    Drop drop = new Drop();
    if (y > height+r) {
      r = random(2, 6); 
      x = random(width);
      y = -r; 
      speed = r;
    }
  }

  // 3.Why didn't work?
  Drop reachedBottom3(Drop self) {
    Drop drop = new Drop();
    if (y > height+r) {
      return drop;
    } else {
      return self;
    }
  }
}

```

---

<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:** [September 19, 2020, 2:19am UTC](https://discourse.processing.org/t/how-to-initialize-class-by-its-own-function/23946/2 "2020-09-19T02:19:43Z")

</div>

The method Drop::**reachedBottom3()** returns a Drop object but you don’t do anything with it.

BtW, the method can be shortened to 1 `return` statement:

```auto
Drop reachedBottom3(final Drop self) {
  return y > height + r? new Drop() : self;
}

```

You need to reassign the returned Drop object back to the array:  
`drops[i] = drops[i].reachedBottom3(drops[i]);`

However that’s pretty awkward b/c the Drop object should be able to restart by its own w/o the caller having to do anything.

Instead you should define a method that **respawn()** the Drop’s fields back to their constructor values.

Basically you move anything inside Drop’s constructor to that separate method and invoke it inside the constructor instead.

And within method **move()** check whether field _y_ has reached the bottom and invoke **respawn()** if so.

Here’s the refactoring I did to your sketch. Check it out:  
https://www.openprocessing.org/sketch/965902/embed/?

Another similar sketch called “Raining Cloud”:  
`http://Studio.ProcessingTogether.com/sp/pad/export/ro.9l84$2z--gWui`

---

<div class="post-metadata">

**Author:** ![IpNeNaEcRe](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ipnenaecre/32/11223_2.png) [@IpNeNaEcRe](https://discourse.processing.org/u/IpNeNaEcRe)\
**Post date:** [September 19, 2020, 9:34pm UTC](https://discourse.processing.org/t/how-to-initialize-class-by-its-own-function/23946/4 "2020-09-19T21:34:30Z")

</div>

Thanks.  
Your refactoring is so simplicity and clean. As a beginner, I learned a lot from it.
