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 & HelpSyntax Questions › static variables
Page Index Toggle Pages: 1
static variables (Read 1597 times)
static variables
Apr 5th, 2009, 8:49pm
 
Hey,
I was wondering if anyone knew of a good way to do static variables in processing. The usual way of just declaring it as such doesn't seem to work, I'm guessing for the same reason static methods don't work. However, I can't think of a way to fake static variables. Thanks.
Re: static variables
Reply #1 - Apr 5th, 2009, 10:35pm
 
what do you mean by static variables?
Re: static variables
Reply #2 - Apr 6th, 2009, 3:37am
 
Global static variables work fine in Processing... Although their usefulness is limited since there will only one PApplet per run anyway.
Indeed, you cannot put static variables in classes in a .pde file. That's because these classes are actually nested classes, internal to the PApplet.
Somehow, you can fake them with so-called global variables (which are actually fields of the PApplet).
Or, you can declare your class in a .java file, but it brings problems of its own.
Re: static variables
Reply #3 - Apr 6th, 2009, 9:49am
 
By static variables I mean a variable who's value is the same across all instances of an object.

I will look into this global variable. It sounds like what I need, which is basically just a list of parameters that every class in my program can access. Thanks.
Re: static variables
Reply #4 - May 5th, 2009, 11:07am
 
All your classes can instanciate the same static class.
Code:

class Something{
 StaticInfo staticThings;  
 Something(){
   staticThings = new StaticInfo();
 }
 void run(){
   println(staticThings.staticData1);
 }
}

static class StaticInfo {
 static String staticData1 = "static data 1";
 static String staticData2 = "static data 2";
}
Re: static variables
Reply #5 - May 5th, 2009, 7:00pm
 
Sorry to interrupt but I'm a little confused as to why this works. I always had the belief that static fields can't be changed. They are static after all. But in this I can. What's all that about? Am I delusional? or have I been assuming a little to much all these years?

void setup()
{
 Something s = new Something();
 s.run();
}

class Something{
 StaticInfo staticThings;  
 Something(){
   staticThings = new StaticInfo();
 }
 void run(){
   println(staticThings.staticData1);

   staticThings.staticData1 = "hey look I can change the static"; //wtf?

   println(staticThings.staticData1);
 }
}

static class StaticInfo {
 static String staticData1 = "static data 1";
 static String staticData2 = "static data 2";
}
Re: static variables
Reply #6 - May 6th, 2009, 3:17am
 
If it's any help I posted a similar question in this thread.

As for static class variables, my understanding is that regardless of how many instances of the class you create, you essentially only create one instance of the variable.  Changing the value of the static variable in one instance affects the value in all other instances - so essentially it's a global variable for instances of the class.  I'm still getting my head round this so not sure if my explanation is technically correct, but that seems to be what happens in practice.

A 'static final' variable however is constant and cannot be changed - it's the 'final' that makes it constant, not 'static' (though I can understand why you'd interpret it that way).  The annoying thing is you can declare static final variables in a class, but you can only declare a non-final static variable in a static class, which apparently may present problems in Processing.  Not sure what kind of problems though and would be interested to know more...
Page Index Toggle Pages: 1