We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › static variables in classes
Page Index Toggle Pages: 1
static variables in classes? (Read 345 times)
static variables in classes?
Dec 14th, 2008, 5:44am
 
Hello,

I've searched the forum and the bug database but could find only one mention of a quirk of processing(?) not allowing static variables in inner classes.

What I want to have is a global ArrayList for a class which is shared among all instances of that class. I don't want to have this ArrayList hovering around globally so I wanted to make it a member of the class, but processing does not allow me to do that. When I make a variable of the class "static" processing says "The field xyz cannot be declared static, static fields can only be declared in static or top level types."

I think this is a Java thing, as we are not working in top level in the processing IDE this is normal.

The only other way of doing this is, as it seems, declaring the class itself static (I don't really know what it changes in classes) but this time, for example if I call the function random() inside class, processing says: "Cannot make a static reference to the non-static method random(...".

So I guess there is no way for me to put that ArrayList inside the class to keep things clean and let the instances of the classes share their encapsulated data, is that true?

Or am I getting this "static" thing in Java all wrong?

Thanks
Re: static variables in classes?
Reply #1 - Dec 14th, 2008, 5:56am
 
Maybe I can create a separate static "holder" class with only static ArrayLists that act as data holders to keep things clean. Does that sound right?
Re: static variables in classes?
Reply #2 - Dec 14th, 2008, 10:27am
 
In Processing, all classes in a .pde are internal to a main, unseen class (of same name as your sketch).
So all classes are internal classes, and it seems you cannot put static variables inside an internal class.

The solution is to put your class in a .java file, which will make it a separate class. But you have to give it an instance of PApplet to be able to use regular Processing functions.
Page Index Toggle Pages: 1