# How to pass non-static variable values?

**URL:** <https://discourse.processing.org/t/how-to-pass-non-static-variable-values/12999>\
**Category:** Processing for Android\
**Created:** [July 26, 2019, 2:39pm UTC](https://discourse.processing.org/t/how-to-pass-non-static-variable-values/12999 "2019-07-26T14:39:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Waboqueox](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/waboqueox/32/5643_2.png) [@Waboqueox](https://discourse.processing.org/u/Waboqueox)\
**Post date:** [July 26, 2019, 2:39pm UTC](https://discourse.processing.org/t/how-to-pass-non-static-variable-values/12999/1 "2019-07-26T14:39:13Z")

</div>

Hi there;

I’m trying to communicate between an activity and mi sketch, but my sketch always expect static values, ¿is there any trick or mode to pass variable values?

Here you have an example:

I can do this in the **activity** :

```auto
  public static int getInterger(){
    int b =(int)(Math.random()*900);
    return b;
  }

```

And receive in the **sketch** :

```auto
    public void draw() {

        background(0);
        ellipse(width / 2, height / 2, MainActivity.getInterger(),MainActivity.getInterger() );

    }

```

But, what I want and I can’t, is to receive non-static values.

This is what **I can’t do** :  
MainActivity:

```auto
    public static float getValue(){
        return (float) (carState.velocity);
    }

```

Sketch:

```auto
public void draw() {
    background(255);
    ellipse(MainActivity.getValue() ,mouseY,100,100);
}

```

carState.velocity have the variable value

Thanks for the help 🤗

---

<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 26, 2019, 3:01pm UTC](https://discourse.processing.org/t/how-to-pass-non-static-variable-values/12999/2 "2019-07-26T15:01:46Z")

</div>

`static` methods are stateless. That is, they can’t access the keyword `this`, either explicitly or implicitly.

In your posted example:

```auto
public static float getValue() {
  return (float) carState.velocity;
}

```

B/c variable _carState_ isn’t declared anywhere in that method, neither as a local variable nor as a method parameter, it’s implied that _carState_ is actually a field belonging to that method’s `class`.

So it has an implicitly hidden `this` keyword: `return (float) this.carState.velocity;`

Which isn’t valid inside `static` methods under Java.

As a workaround, your method can request a MainActivity parameter in order to access _carState_:

```auto
public static float getValue(final MainActivity mainAct) {
  return (float) mainAct.carState.velocity;
}

```

Or more directly as a parameter of the same datatype as _carState_ instead:

```auto
public static float getValue(final DatatypeOfCarState carState) {
  return (float) carState.velocity;
}

```

---

<div class="post-metadata">

**Author:** ![Waboqueox](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/waboqueox/32/5643_2.png) [@Waboqueox](https://discourse.processing.org/u/Waboqueox)\
**Post date:** [July 29, 2019, 7:41am UTC](https://discourse.processing.org/t/how-to-pass-non-static-variable-values/12999/3 "2019-07-29T07:41:04Z")

</div>

@GoToLoop thank you so much, couldn’t try till today.

Passing the MainActivity I **can get the value** ; with the parameter is a little bit harder (other person code is implicated, and that complicate the things) and in this way is working perfectly 😊

Thanks again 🤗
