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 › One class-related question - static field
Page Index Toggle Pages: 1
One class-related question - static field (Read 475 times)
One class-related question - static field
Jun 12th, 2009, 11:10pm
 
I wanted to declear several class variables but I keep getting error:
The field spacingX cannot be declared static; static fields can only be declared in static or top level types.

WHY? I read all the examples in processing and the link to java's static fields and methods but even the java's example didn't work.

public class buttons
{
 public int left;
 public int top;
 private static int spacingX, spacingY;
}
Re: One class-related question - static field
Reply #1 - Jun 13th, 2009, 3:09am
 
any classes in your processing code actually get included as an internal class within your PApplet. which means you can't use statics.

try selecting export from the menu and then examine the resulting .java file and you'll see what it's doing.
Re: One class-related question - static field
Reply #2 - Jun 13th, 2009, 4:57am
 
Yes, it is an annoying limitation of Java.
Somehow, static variables are similar to global variables, so you can just declare them at sketch level (before setup() or before you class definition. It is ugly and doesn't respect encapsulation (hiding these variables from other classes if needed) but it is simple.
Or you declare your classes in .java files, the classes will be top-level classes instead of nested ones, with more possibilities (but some restrictions, like needing a PApplet reference to use Processing functions).
Page Index Toggle Pages: 1