# Why can't I create my class after I declare a variable with it?

**URL:** <https://discourse.processing.org/t/why-cant-i-create-my-class-after-i-declare-a-variable-with-it/41884>\
**Category:** Beginners\
**Created:** [May 1, 2023, 2:12pm UTC](https://discourse.processing.org/t/why-cant-i-create-my-class-after-i-declare-a-variable-with-it/41884 "2023-05-01T14:12:25Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hha2011](https://avatars.discourse-cdn.com/v4/letter/h/3bc359/32.png) [@Hha2011](https://discourse.processing.org/u/Hha2011)\
**Post date:** [May 1, 2023, 2:12pm UTC](https://discourse.processing.org/t/why-cant-i-create-my-class-after-i-declare-a-variable-with-it/41884/1 "2023-05-01T14:12:25Z")

</div>

\<

```auto
Ball ball;

class Ball {
  
}

```

> 

I swear this code worked in processing 4.1.1 or maybe 4.0b7 cause that was how the tutorial worked(well the class is in a different file but processing reads it like tht so it does not matter) can someone tell me what changed, please

---

<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:** [May 1, 2023, 2:16pm UTC](https://discourse.processing.org/t/why-cant-i-create-my-class-after-i-declare-a-variable-with-it/41884/2 "2023-05-01T14:16:30Z")

</div>

I this the entire Sketch?

I think that you need setup and draw to run it.

Also, I think you can’t save the Sketch with a name that is also a class name. So save it as Ball1 or BallExample

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [May 1, 2023, 2:40pm UTC](https://discourse.processing.org/t/why-cant-i-create-my-class-after-i-declare-a-variable-with-it/41884/3 "2023-05-01T14:40:32Z")

</div>

Hi

[![](https://img.youtube.com/vi/lmgcMPRa1qw/hqdefault.jpg "8.2: Defining a Class Part I - Processing Tutorial") ](https://www.youtube.com/watch?v=lmgcMPRa1qw)

[![](https://img.youtube.com/vi/XwfOVFelLoo/hqdefault.jpg "8.3: Defining a Class Part II - Processing Tutorial") ](https://www.youtube.com/watch?v=XwfOVFelLoo&t=439s)

---

<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:** [May 1, 2023, 3:55pm UTC](https://discourse.processing.org/t/why-cant-i-create-my-class-after-i-declare-a-variable-with-it/41884/4 "2023-05-01T15:55:54Z")

</div>

> [@Hha2011](#):
>
> the class is in a different file

main pde and the pde with the class must be in the same folder

> [@Hha2011](#):
>
> but processing reads it like tht so it does not matter

so, processing does something?

Do you receive an error message or what happens?

---

<div class="post-metadata">

**Author:** ![Hha2011](https://avatars.discourse-cdn.com/v4/letter/h/3bc359/32.png) [@Hha2011](https://discourse.processing.org/u/Hha2011)\
**Post date:** [June 7, 2023, 6:19pm UTC](https://discourse.processing.org/t/why-cant-i-create-my-class-after-i-declare-a-variable-with-it/41884/6 "2023-06-07T18:19:16Z")

</div>

Draw or setup is not required, since you can run an empty sketch

---

<div class="post-metadata">

**Author:** ![Hha2011](https://avatars.discourse-cdn.com/v4/letter/h/3bc359/32.png) [@Hha2011](https://discourse.processing.org/u/Hha2011)\
**Post date:** [June 7, 2023, 6:20pm UTC](https://discourse.processing.org/t/why-cant-i-create-my-class-after-i-declare-a-variable-with-it/41884/7 "2023-06-07T18:20:02Z")

</div>

Those were the exact tutorials i watched

---

<div class="post-metadata">

**Author:** ![Hha2011](https://avatars.discourse-cdn.com/v4/letter/h/3bc359/32.png) [@Hha2011](https://discourse.processing.org/u/Hha2011)\
**Post date:** [June 7, 2023, 6:21pm UTC](https://discourse.processing.org/t/why-cant-i-create-my-class-after-i-declare-a-variable-with-it/41884/8 "2023-06-07T18:21:49Z")

</div>

Yes, they were in the same folder and processing reads files in the order: First the main file and then the other files in alphabetical order

---

<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:** [June 8, 2023, 5:51am UTC](https://discourse.processing.org/t/why-cant-i-create-my-class-after-i-declare-a-variable-with-it/41884/9 "2023-06-08T05:51:57Z")

</div>

> [@Chrisir](#):
>
> think that you need setup and draw to run it.
> 
> Also, I think you can’t save the Sketch with a name that is also a class name. So save it as Ball1 or BallExample

I think that you need setup and draw to run it.

Also, I think you can’t save the Sketch with a name that is also a class name. So save it as Ball1 or BallExample

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [June 8, 2023, 6:12am UTC](https://discourse.processing.org/t/why-cant-i-create-my-class-after-i-declare-a-variable-with-it/41884/10 "2023-06-08T06:12:11Z")

</div>

> [@Hha2011](#):
>
> ```auto
> Ball ball;
> 
> class Ball {
>   
> }
> 
> ```
> 
> I swear this code worked in processing 4.1.1 or maybe 4.0b7 cause that was how the tutorial worked(well the class is in a different file but processing reads it like tht so it does not matter) can someone tell me what changed, please

I can’t imagine that this has worked in processing not in p3 and not in p4.

You are using something called static mode (without setup/draw) and you can only have simple instructions for it.

Other mode is active mode (with setup and draw).

The reason, why it isn’t work is how processing assembles the result.  
In static mode it wraps everything inside

```auto
public class sketch_static extends PApplet {
 public void setup() {
   // Static mode code goes here
 }
// Some other code like settings
}

```

The opposite on active mode

```auto
public class sketch_active extends PApplet {
   // all active mode code (*.pde) goes here
}

```

means your code would work in active mode but not in static mode.

And also consider this from @Chrisir …

> [@Chrisir](#):
>
> Also, I think you can’t save the Sketch with a name that is also a class name. So save it as Ball1 or BallExample

Cheers  
— mnse

---

<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:** [June 8, 2023, 1:07pm UTC](https://discourse.processing.org/t/why-cant-i-create-my-class-after-i-declare-a-variable-with-it/41884/11 "2023-06-08T13:07:29Z")

</div>

> [@Hha2011](#):
>
> ```auto
> Ball ball;
> 
> class Ball {
>   
> }
> 
> ```

```auto
class Ball {
}

Ball ball = new Ball();
println(ball);

exit();

```

---

<div class="post-metadata">

**Author:** ![Hha2011](https://avatars.discourse-cdn.com/v4/letter/h/3bc359/32.png) [@Hha2011](https://discourse.processing.org/u/Hha2011)\
**Post date:** [June 9, 2023, 12:13pm UTC](https://discourse.processing.org/t/why-cant-i-create-my-class-after-i-declare-a-variable-with-it/41884/12 "2023-06-09T12:13:58Z")

</div>

Yes, that would be the solution but im trying to avoid defining it before everything else

---

<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:** [June 9, 2023, 12:39pm UTC](https://discourse.processing.org/t/why-cant-i-create-my-class-after-i-declare-a-variable-with-it/41884/13 "2023-06-09T12:39:27Z")

</div>

You need setup and draw anyway so just enter these and tty this…?

---

<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:** [June 9, 2023, 5:43pm UTC](https://discourse.processing.org/t/why-cant-i-create-my-class-after-i-declare-a-variable-with-it/41884/14 "2023-06-09T17:43:10Z")

</div>

> [@Hha2011](#):
>
> … but I’m trying to avoid defining it before everything else.

When we click the “Run” ▶ button, the PDE concatenates all the “.pde” files as 1 “.java” file, and they’re also wrapped up inside 1 PApplet subclass.

My “fixed” posted example would become something like this “.java” file:

> [@GoToLoop](#):
>
> ```auto
> class Ball {
> }
> 
> Ball ball = new Ball();
> println(ball);
> 
> exit();
> 
> ```

```auto
// ...

public class sketch_static extends PApplet {
  public void setup() {
    class Ball {
    }

    Ball ball = new Ball();
    println(ball);

    exit();
    noLoop();
  }

  static public void main(String[] passedArgs) {
    // ...
  }
}

```

So the whole “static” code, besides being wrapped up in 1 PApplet subclass, it’s also been moved inside callback method **setup()**.

Although Java doesn’t allow us defining a method inside another method, surprisingly we can define classes inside methods!

In such case the order is important, b/c we have to define an internal class before we could instantiate it via `new`.
