# Object-oriented theory doubt. sin() function, oscillation

**URL:** <https://discourse.processing.org/t/object-oriented-theory-doubt-sin-function-oscillation/36751>\
**Category:** Beginners\
**Tags:** homework\
**Created:** [May 6, 2022, 3:40pm UTC](https://discourse.processing.org/t/object-oriented-theory-doubt-sin-function-oscillation/36751 "2022-05-06T15:40:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erif](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/erif/32/15932_2.png) [@Erif](https://discourse.processing.org/u/Erif)\
**Post date:** [May 6, 2022, 3:40pm UTC](https://discourse.processing.org/t/object-oriented-theory-doubt-sin-function-oscillation/36751/1 "2022-05-06T15:40:49Z")

</div>

Hi all,  
My understanding of OOP is that in order to create a Class, you need to define 1) variables, 2) constructor and 3) methods. In the example below though, variables x and theta are 1) defined, their values assigned in 2) the constructor, however the x formula to determine its value must be placed as a method. If I put it in the constructor the oscillator won’t move, but if I move it inside void.display() it will.  
Why is that??  
Thanks!

```auto
class Oscillator {
  //Variables
  float theta; //oscillation angle
  float x;
  
  //Constructor
  Oscillator() {
    theta = 0;
  }
  
  //Methods
  void move() {
    //Increment theta with each cycle to create movement
    theta += 0.05;
  }

  void display() {
    //draw the ellipse at the value produced by sine
    x = map(sin(theta),-1,1,0,width); // this can't go in the constructor?!?
    
    fill(0);
    stroke(0);
    line(width/2,0,x,height/2);
    ellipse(x,height/2,20,20);
  
  }
}
```

---

<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:** [May 6, 2022, 3:45pm UTC](https://discourse.processing.org/t/object-oriented-theory-doubt-sin-function-oscillation/36751/2 "2022-05-06T15:45:39Z")

</div>

> [@Erif](#):
>
> If I put it in the constructor the oscillator won’t move, but if I move it inside void.display() it will.

A class constructor is called once only, when it’s instantiated.  
A method can be called indefinitely.

---

<div class="post-metadata">

**Author:** ![Erif](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/erif/32/15932_2.png) [@Erif](https://discourse.processing.org/u/Erif)\
**Post date:** [May 6, 2022, 8:06pm UTC](https://discourse.processing.org/t/object-oriented-theory-doubt-sin-function-oscillation/36751/3 "2022-05-06T20:06:14Z")

</div>

Got it, thank you! Cheers
