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 Member functions in Processing
Page Index Toggle Pages: 1
Static Member functions in Processing (Read 434 times)
Static Member functions in Processing
Oct 14th, 2007, 1:10pm
 
I've been trying to include some static member functions and variables in a class in Processing, but it invariably disallows me to do that. I just need some help regarding the implementation of something like this:

class Something
{
 int a;
 static int B;

 static int GetB()
 {
   return B;
 }

 static void PrintHello()
 {
   println("Hello from class Something");
 }

}


Can somebody tell me what wrong I'm doing and how to correct that?
Re: Static Member functions in Processing
Reply #1 - Oct 14th, 2007, 7:19pm
 
Well, you're doing nothing wrong from a _Java_ point of view.  Unfortunately due to the way Processing code is preprocessed into Java code, your classes can't have static functions.

Basically, Processing turns any class declared in the IDE into an inner class - the wrapper class extends PApplet, and that is the way you get to say things like println("Hello") instead of System.out.println("Hello") or PApplet.println("Hello").  So what your example is parsed into behind the scenes is something like this:

Code:


import [a whole bunch of things];

public class MyClass extends PApplet{

[other stuff, like your setup or draw methods]

class Something{
int a;
static int B;

static int GetB(){
return B;
}

static void PrintHello(){
println("Hello from class Something");
}
}

}



Now, as to why exactly this is disallowed, that's a whole other story.  There are some (kind of) decent reasons that nested classes are not allowed to have static members, which you can certainly Google for if you're interested.  I'm not sure what the argument is against static methods in inner classes, but apparently Sun decided it was a Bad Idea.

There are two workarounds in Processing: first, you could just declare any of those methods as global functions, outside of your declared class, which will probably suit your needs 90% of the time.  For instance, you could write:

int GetB(){
 return Something.B;
}

and

void PrintHello(){
 println("Hello from class Something");
}

in your example, outside of the Something class definition, and things should work right.

Otherwise, if you create a new tab in Processing and name it  something with a .java extension, it will not be run through the preprocessor, and you can write pure Java code.  The "gotcha" there is that the Something class will no longer extend PApplet, so you'd need to write PApplet.println("Hello") instead of just println("Hello").  Similarly, a lot of the math functions will need to be qualified like that as well, and the drawing ones might be even trickier, since I think they even require a specific instance of PApplet, which you'll need to store in the Something class when you construct it.
Re: Static Member functions in Processing
Reply #2 - Oct 14th, 2007, 11:27pm
 
this is also covered in Help -> Getting Started
http://processing.org/reference/environment/index.html#Tabs
Page Index Toggle Pages: 1