# Method from class does not work

**URL:** <https://discourse.processing.org/t/method-from-class-does-not-work/34354>\
**Category:** Beginners\
**Created:** [December 29, 2021, 3:50pm UTC](https://discourse.processing.org/t/method-from-class-does-not-work/34354 "2021-12-29T15:50:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![marina\_kdot](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/marina_kdot/32/15322_2.png) [@marina\_kdot](https://discourse.processing.org/u/marina_kdot)\
**Post date:** [December 29, 2021, 3:50pm UTC](https://discourse.processing.org/t/method-from-class-does-not-work/34354/1 "2021-12-29T15:50:39Z")

</div>

I try to set up a short array and display four squares with random position and size. But I always get a syntax error. I spend lots of time comparing my code to other examples and they are all running, but mine is not. 🧐 Do you see what I did wrong here?

```auto
A[] squares = new A[4];

void setup() {
  size(600, 600);
  for(int i = 0; i < 4; i++) {
    squares[i] = new A(x, y, w, h);
  }
}

void draw() {
  background (250);
  for (A s : squares) {
    do();
    display();
  } 
}

class A {
  float x, y, w, h;
  
  void do() {
    x = random(20, 300);
    y = random(40, 500);
    w = random(100, 200);
    h = random(30, 110);
  }
  
  void display() {
    rect(x, y, w, h);
  }
}

```

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [December 29, 2021, 4:18pm UTC](https://discourse.processing.org/t/method-from-class-does-not-work/34354/2 "2021-12-29T16:18:16Z")

</div>

Hello,

References:

- [Objects / Processing.org](https://processing.org/tutorials/objects)
- [https://processing.org/reference/class.html](https://processing.org/reference/class.html)
- [https://processing.org/reference/Object.html](https://processing.org/reference/Object.html)
- [Array Objects / Examples / Processing.org](https://processing.org/examples/arrayobjects.html)

Your code with modifications:

```auto
A[] squares = new A[4];

void setup() {
  size(600, 600);
  for (int i = 0; i < 4; i++) {
    squares[i] = new A();
  }
}

void draw() {
  background (250);
  for (A s : squares) {
    s.doit();
    s.display();
  }
}

class A {
  float x, y, w, h;

  void doit() {
    x = random(20, 300);
    y = random(40, 500);
    w = random(100, 200);
    h = random(30, 110);
  }

  void display() {
    rect(x, y, w, h);
  }
}

```

I only took a quick look at this to get you started… modify as required.

It did not seem to like _do()_ (this may be reserved) so changed it to _doit()_.

Consider adding a constructor to your class and make an array of rectangle data in setup().

It will take some time to get comfortable with [OOP](https://en.wikipedia.org/wiki/Object-oriented_programming).

`:)`

---

<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, 2021, 10:45pm UTC](https://discourse.processing.org/t/method-from-class-does-not-work/34354/3 "2021-12-29T22:45:23Z")

</div>

> [@marina\_kdot](#):
>
> ```auto
> do();
> display();
> 
> ```

Yeah, you need `s.` before these

Because they are hidden inside the class, we need to name an object to call those functions (or methods).

When we just say do or display, it would refer to functions outside the class, functions like draw() or setup() that you don’t have.

Chrisir
