# Java: create objects and call their methods in loops

**URL:** <https://discourse.processing.org/t/java-create-objects-and-call-their-methods-in-loops/1515>\
**Category:** Coding Questions\
**Created:** [July 5, 2018, 10:58am UTC](https://discourse.processing.org/t/java-create-objects-and-call-their-methods-in-loops/1515 "2018-07-05T10:58:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/brian/32/674_2.png) [@Brian](https://discourse.processing.org/u/Brian)\
**Post date:** [July 5, 2018, 10:58am UTC](https://discourse.processing.org/t/java-create-objects-and-call-their-methods-in-loops/1515/1 "2018-07-05T10:58:07Z")

</div>

I’m trying to use a loop to create 10 objects at a time then use another loop in Draw() method to call the objects’ render() methods.  
At first I tried creating and calling objects method manually and it worked. Now I want to do that with loops, I’m not sure why the program shows error.

```auto
void setup() {
  size(600, 320);
}

int[] avVal = {-1, 1};

class Obj { //<---------my object class
  int x;
  int y;

  void render() {
    int ran1 = int(random(2));
    int ran2 = int(random(2));
    int vx = int(random(2));
    int vy = int(random(2));
    this.x += vx*avVal[ran1];
    this.y += vy*avVal[ran2];
    point(this.x, this.y);
  }
}

Obj[] arr = new Obj[10]; //<---------create an array to contain 10 objects

for (int i = 0; i < 10; i++) { //<---------loop to add 10 objects to array
  Obj ob = new Obj();
  ob.x = int(random(600)); //<---------set random starting coordinates for objects
  ob.y = int(random(320)); //<--------/
  arr[i] = ob;
}

**_get error underlined here: Error on "VariableDeclarators"_**
 \/ \/
void draw() {
  for (int u = 0; u < 10; u++) { //<---------this loop to iterate through the array
    arr[u].render(); //<---------call each object in array to draw
  }
}

```

---

<div class="post-metadata">

**Author:** ![Honny](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/honny/32/672_2.png) [@Honny](https://discourse.processing.org/u/Honny)\
**Post date:** [July 5, 2018, 11:31am UTC](https://discourse.processing.org/t/java-create-objects-and-call-their-methods-in-loops/1515/2 "2018-07-05T11:31:54Z")

</div>

your for loop it is outside of any methods.  
assuming your arr variable it is global and you want create 10 objects in the setup function you can arrange your code as this :

```auto

Obj[] arr = new Obj[10];
int[] avVal = {-1, 1};

void setup() 
{
  size(600, 320);
  
  for (int i = 0; i < 10; i++) 
  { 
    Obj ob = new Obj();
    ob.x = int(random(600)); 
    ob.y = int(random(320)); 
    arr[i] = ob;
  }
}

void draw() 
{
  for (int u = 0; u < 10; u++) 
  { 
    arr[u].render(); 
  }
}

class Obj 
{
  int x;
  int y;

  void render() 
  {
    int ran1 = int(random(2));
    int ran2 = int(random(2));
    int vx = int(random(2));
    int vy = int(random(2));
    this.x += vx*avVal[ran1];
    this.y += vy*avVal[ran2];
    point(this.x, this.y);
  }
}

```

---

<div class="post-metadata">

**Author:** ![Brian](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/brian/32/674_2.png) [@Brian](https://discourse.processing.org/u/Brian)\
**Post date:** [July 5, 2018, 12:30pm UTC](https://discourse.processing.org/t/java-create-objects-and-call-their-methods-in-loops/1515/3 "2018-07-05T12:30:35Z")

</div>

I don’t quite understand why we have to put the for loop in the setup function, can you help me with that? Anyway, thanks alot.

---

<div class="post-metadata">

**Author:** ![mat650](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mat650/32/564_2.png) [@mat650](https://discourse.processing.org/u/mat650)\
**Post date:** [July 5, 2018, 12:35pm UTC](https://discourse.processing.org/t/java-create-objects-and-call-their-methods-in-loops/1515/4 "2018-07-05T12:35:48Z")

</div>

That is necessary to instantiate the class Obj and store the objects (instances) in the array of type Obj.

---

<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:** [July 5, 2018, 5:04pm UTC](https://discourse.processing.org/t/java-create-objects-and-call-their-methods-in-loops/1515/5 "2018-07-05T17:04:33Z")

</div>

> [@Brian](#):
>
> I don’t quite understand why we have to put the `for ()` loop in the **setup()** function, …

B/c outside of methods, we can only declare fields. 🙋‍♂️  
However, we can use initialization curly blocks `{}` outside methods and run statements there: 👨‍🎤

```auto
final Obj[] arr = new Obj[10];

{
  for (int i = 0; i < arr.length; ++i) { 
    final Obj ob = arr[i] = new Obj();
    ob.x = (int) random(600); 
    ob.y = (int) random(320);
  }
}

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

  // blah, blah, blah...
}

class Obj {
  int x, y;

  void render() {
    // blah, blah, blah...
  }
}

```

---

<div class="post-metadata">

**Author:** ![cast1163](https://avatars.discourse-cdn.com/v4/letter/c/a5b964/32.png) [@cast1163](https://discourse.processing.org/u/cast1163)\
**Post date:** [July 5, 2018, 5:51pm UTC](https://discourse.processing.org/t/java-create-objects-and-call-their-methods-in-loops/1515/6 "2018-07-05T17:51:39Z")

</div>

Hello Brian

I think your program is correct and working normally. Maybe it is necessary to put the loop for in the setup(), since it is about an initialization

Hello from France

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [July 5, 2018, 6:03pm UTC](https://discourse.processing.org/t/java-create-objects-and-call-their-methods-in-loops/1515/7 "2018-07-05T18:03:28Z")

</div>

🤢 That’s awful. Let’s not teach people to do that, ok?

---

<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:** [July 5, 2018, 6:09pm UTC](https://discourse.processing.org/t/java-create-objects-and-call-their-methods-in-loops/1515/8 "2018-07-05T18:09:17Z")

</div>

Not OK! I’m against withholding information! 🤐  
He asked why he couldn’t do it; and I simply replied w/ what I knew about it! 📖
