Using class-wide methods in non-static classes

Ok, this is what you can do:

Close Processing. Create the following folder structure

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

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

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

void draw() {
  background(0);
}

Java file

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

1 Like