# Using class-wide methods in non-static classes

**URL:** https://discourse.processing.org/t/using-class-wide-methods-in-non-static-classes/1677
**Category:** Coding Questions
**Created:** [July 11, 2018, 1:53am UTC](https://discourse.processing.org/t/using-class-wide-methods-in-non-static-classes/1677 "2018-07-11T01:53:54Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Jake](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@Jake](https://discourse.processing.org/u/Jake)
#### Post date: [July 11, 2018, 1:53am UTC](https://discourse.processing.org/t/using-class-wide-methods-in-non-static-classes/1677/1 "2018-07-11T01:53:54Z")

</div>

After doing some research, I have yet to find a way to use static methods in a non-static class. Alternatively, I can’t create instances of an object of a static class. I did notice someone mention class-wide methods, but there was no information on the processing wiki.

Basically, all I want is a class that allows me to create objects, but also allows me to use static methods. For example if the class was named “Car”, I want to be able to say

Car c1 = new Car();

but also be able to write a method that is called as such:

Car.functionName();

I know this is possible in Java, but Processing 3 seems to be preventing me from doing it. Any help would be appreciated, thank you in advance!

---

<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 11, 2018, 2:10am UTC](https://discourse.processing.org/t/using-class-wide-methods-in-non-static-classes/1677/2 "2018-07-11T02:10:51Z")

</div>

B/c all classes inside a “.pde” file are nested inside a PApplet subclass, if we need to have `static` members inside them, we’re gonna need to declare those classes as `static` as well:

> **[Deeply-layered nested classes](https://Discourse.Processing.org/t/deeply-layered-nested-classes/805)**
>
> Hello. I’m trying to make nested classes to make some sort of system in my classes. I am working insode Visual Studio Code, where you can hide lines of code. When I try: static class A{ static class B{ class C{ C(){} ...

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [July 11, 2018, 8:20am UTC](https://discourse.processing.org/t/using-class-wide-methods-in-non-static-classes/1677/3 "2018-07-11T08:20:40Z")

</div>

Ok, this is what you can do:

Close Processing. Create the following folder structure

```auto
C:\P3> tree ApolloMain /a /f

C:\P3\APOLLOMAIN
    ApolloMain.pde
    AutoCar.java

```

You have a folder called ApolloMain and inside you find a pde and a java file. Open Processing and open the pde. You will see that ApolloMain is one of your tabs and `AutoCar.java` is another tab. Now add the following code:

## File extension pde

```java
AutoCar car;
void setup() {
  size(600, 400);

  car=new AutoCar();
  println("Get some info: "+car.whatIsMyPlanet());
}

void draw() {
  background(0);
}

```

## Java file

```java
class AutoCar {

  static String PLANET="MARS";

  static String whatIsMyPlanet() {
    return PLANET;
  }

  AutoCar() {
  }
}

```

Run it. If you want to understand a bit more about what Processing in doing, go to `file` menu in the PDE and export the code. Then have a look at the generated Java files.

Kf

---

<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 11, 2018, 12:03pm UTC](https://discourse.processing.org/t/using-class-wide-methods-in-non-static-classes/1677/4 "2018-07-11T12:03:12Z")

</div>

Nice step-by-step @kfrajer. However, we can exceptionally have `static` fields inside inner classes as long as they’re `final` & either primitive or String; b/c in this case, they become compile-time constants: 😀

```auto
void setup() {
  final AutoCar car = new AutoCar();
  println("Planet:", car); // Planet: MARS
  exit();
}

class AutoCar {
  static final String PLANET = "MARS"; // Compile-time constant

  @Override String toString() {
    return PLANET;
  }
}

```
