# Cannot refer to the non-final local variable ... defined in an enclosing scope

**URL:** <https://discourse.processing.org/t/cannot-refer-to-the-non-final-local-variable-defined-in-an-enclosing-scope/8023>\
**Category:** Coding Questions\
**Created:** [February 1, 2019, 4:22pm UTC](https://discourse.processing.org/t/cannot-refer-to-the-non-final-local-variable-defined-in-an-enclosing-scope/8023 "2019-02-01T16:22:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Renaud](https://avatars.discourse-cdn.com/v4/letter/r/dc4da7/32.png) [@Renaud](https://discourse.processing.org/u/Renaud)\
**Post date:** [February 1, 2019, 4:22pm UTC](https://discourse.processing.org/t/cannot-refer-to-the-non-final-local-variable-defined-in-an-enclosing-scope/8023/1 "2019-02-01T16:22:12Z")

</div>

Hi, I am trying a code found on the internet (below). When I run it, I obtain this message:  
“cannot refer to the non-final local variable startw defined in an enclosing scope”  
What is it supposed to mean ? What can I do to solve this ?  
Thanks in advance!  
Renaud

```auto
float startw;  
startw = 15 + random(50);

class Branch {

  float xp1;
  float xp2;
  float yp1;
  float yp2;

  void render() {

    float hyp = sqrt(pow((xp2 - xp1), 2) + pow((yp2 - yp1), 2));
    for (float i=0; i < hyp; i+=3) {
      rectMode(CENTER);
      noStroke();
    };
    startw = (startw - (hyp * ((startw - pow(startw, 0.9))/hyp)));

    popMatrix();
  };
};

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [February 1, 2019, 4:41pm UTC](https://discourse.processing.org/t/cannot-refer-to-the-non-final-local-variable-defined-in-an-enclosing-scope/8023/2 "2019-02-01T16:41:20Z")

</div>

> [@Renaud](#):
>
> I am trying a code found on the internet

not every code is worth copying

pls. start over with your own code  
after a description of steps what you want to do…

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [February 1, 2019, 4:47pm UTC](https://discourse.processing.org/t/cannot-refer-to-the-non-final-local-variable-defined-in-an-enclosing-scope/8023/3 "2019-02-01T16:47:23Z")

</div>

I think the code above is an example of something not worth copying. Even if you solve this problem your program will crash because there is no matching pushMatrix() for the popMatrix()

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [February 5, 2019, 3:26am UTC](https://discourse.processing.org/t/cannot-refer-to-the-non-final-local-variable-defined-in-an-enclosing-scope/8023/4 "2019-02-05T03:26:18Z")

</div>

First, above comments are correct – that code is trash. Start by defining your problem, ask for help on that.

> [@Renaud](#):
>
> “cannot refer to the non-final local variable startw defined in an enclosing scope”

If your sketch uses classes and functions (like `draw()` and `setup()`) then don’t put lines of code outside a function.

Don’t do this:

```auto
float foo;
foo = 15 + random(50); // DON'T compute outside a function / method / class!
void draw(){
  println(foo);
}

```

but instead do this:

```auto
float foo;
void setup(){
  foo = 15 + random(50); // DO compute inside a function / method / class!
}
void draw(){
  println(foo);
}

```

---

<div class="post-metadata">

**Author:** ![Renaud](https://avatars.discourse-cdn.com/v4/letter/r/dc4da7/32.png) [@Renaud](https://discourse.processing.org/u/Renaud)\
**Post date:** [February 5, 2019, 7:51am UTC](https://discourse.processing.org/t/cannot-refer-to-the-non-final-local-variable-defined-in-an-enclosing-scope/8023/5 "2019-02-05T07:51:09Z")

</div>

All right ! Thanks for your help !
