How to get the sketchPath from a static method


#1

Hi, I’ve tried everything I could think of to try to load a file inside an enum, but can’t get a way to reference to the current sketchPath statically. Does anyone know any posible solution?

That’s a small part of the code, but hopefully it’d be enough for you to understand the problem.

import java.io.File;
public enum SoundEffect {
   BELL("bell.wav"),
   BLOWER("blower.wav");

   // Each sound effect has its own clip, loaded with its own sound file.
   private Clip clip;

   // Constructor to construct each element of the enum with its own sound file.
   SoundEffect(String soundFileName) {
      try {
         // Use URL (instead of File) to read from disk and JAR.
         File soundFile = new File(soundFileName);

Thank you!


#2

Static code can only access static members. :zipper_mouth_face:
Or else, request a non-static reference via a method/constructor’s parameter. :flushed:


#3

tbh I don’t know why sketchPath() isn’t a static method, but in the meantime I’m using the constructor method, but it’s a huge overhead as I had to make a container class for the enum, as I couldn’t get it to work with the enum itself :S


#4

B/c each instance of PApplet can have its own sketchPath folder.

If the field sketchPath were static, each instance of PApplet would be obliged to share the same folder path in order to save & load files.


#5

Seems fair enough, but why would you want to have multiple PApplet instances on the same instance of java?


#6

If you use multiple windows, you will have multiple applet instances. They have their own path assigned internally. Notice this path location can be “overriden” meaning you can ensure all instantiated applets points to the same sketch by providing extra arguments during their construction. This should be done if they want to get the access the same pool of user resources aka. images, fonts, etc. Why is this design like this? No clue. However, it is not that bad. At least, you can change the default path definition, if needed.

Kf


#7

However in P3, the devs decided to slap a private access keyword to the field sketchPath. :face_with_symbols_over_mouth:

Once sketchPath is set, we can’t change it. Unless we use reflection on it. :angry:


#8

@GoToLoop I’m sure the Developers have been working really hard to make the source code as flexible as possible


#9

I’m a forum goer since Processing 1.5.1.
What I see is that devs’ decisions don’t take the forum into account at all! :disappointed:


#10

I still wonder, under what circumpstances would you have multiple window applets assigned to different sketches but running under the same instance of java :S


#11

It’s a matter of flexibility. They could easily declare field sketchPath as static. :stuck_out_tongue:
In such case, the last created [p]PApplet[/u] would determine the sketchPath for all PApplet instances. :crazy_face: