We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I need to use a static method/variable in a non static class, but because it is an inner class I can't. Is there a way around this? If I make my classes in .java files they are top level classes but then I have to go through the hassle of importing any libs I need access to and it becomes increasingly difficult to work with the processing draw functions and access global variables created in the main tab of the sketch. I understand the uselessness of having a static method in a non-static inner class but I don't even know what the outer class is or what it's doing.
Example Class
class Example{
public static boolean iNeedHelp = true;
Example(){
print("The class needs to be non-static");
}
}
Attempting to compile and run this code just throws "The field iNeedHelp cannot be declared static in a non-static inner type, unless initialized with a constant expression"
EDIT: Found some info that I think partially answers my question here, but how would I go about passing an instance of the main class into one of my .java files? https://processing.org/discourse/beta/num_1233971698.html
Answers
through the constructor.
see this https://processing.org/tutorials/eclipse/ specifically the "Processing in Eclipse with Multiple Classes" bit
There are some approaches for it. For example, you may simply declare your nested
class
asstatic
:static class Example {}
Or you may create another
static class
andextends
it in your actualclass
: :ar!If you can't either use
extends
or prefer 1class
only, but can't declare itstatic
b/c you wanna access Processing's non-static API members inside yourclass
, you're gonna need to request the sketch's PApplet reference in its constructor and store it in some field, so you can call Processing's methods & access its fields there too: #:-SI didn't make the connection that because the sketch gets compiled into a single class (aside from any included .java files) you can use 'this' to pass the PApplet into another class. I'm also still not entirely sure how the PApplet works but I understand it enough to get my sketch working again. Thank you for the speedy replys!
class
whichextends
PApplet. L-)class
and are free to access all Processing's API w/o any explicit reference. :-bdclass
asstatic
, it behaves pretty much like any regular topclass
from ".java" tab files. :ar!That makes sense. Thank you!
Ummmm... cannot find a class or type named "PApplet"
EDIT: You need to import it!
Another thing to note is that this doesn't give the class access to global variables you create, only the processing static functions (as far as I can tell)
My 2 examples are supposed to just copy & paste & run on the PDE (Processing's IDE).
If you end up needing to
import
PApplet, then it's a ".java" file rather than a ".pde" 1. :-@As a hint, try exporting a sketch and looking in the exported directory for the Java file that gets created, you'll see what the preprocessor does and that might help you get at the global variables (which i think will just become member variables of the outer class, but i haven't tested that and am not sure it'll help)
I don't think you will have access to global variables from a static context or from a java class. I mean, those global variables in the main tab (or visible to all other pde tabs) are encapsulated in the main PApplet instance. Your class cannot access them directly but you can provide methods to retrieve this data or keep them updated. Without knowing about your final requirment, I can only provide one approach using @GoToLoop's prev post.
Below, myNumber is what we know as a global variable.
Kf
I just made a static field for each of the ones my class needed and created a static init method to call once they were set. They don't change so it worked pretty well.