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 class variables
Pages: 1 2 
static class variables (Read 6301 times)
static class variables
Jan 11th, 2010, 11:20am
 
When I try to make a static class variable I get the following error:

The field variableName cannot be declared static; static fields can only be declared in static or top level types.

A snippet of code is:
public class myClass {
  static int variableName;
  // blah
}

I'm not a java programmer per-se, but I have OOP experience in C++.  I'm guessing this is reminding me that Processing is actually running as a class under Java and hence any class I create within Processing is a "sub-class" of the top-level Processing class. So my ability to use static variables (as opposed to static constants ala the "final" keyword) is at an impasse.

Can anyone verify and/or tell me how to fix this problem?

Thanks

Mike
Re: static class variables
Reply #1 - Jan 11th, 2010, 11:36am
 
Just put your class in a tab and save this tab as a Java file (name it myClass.java for example). This will compile your class as an external one, and not as an PApplet inner-class.
Re: static class variables
Reply #2 - Jan 11th, 2010, 2:05pm
 
ahhhh.... I did put my class in a tab, but let it default to a pde file.

I already started a work-around (globals!!! gack!), but I'll try your technique later.

Thanks

Mike
Re: static class variables
Reply #3 - Jan 11th, 2010, 3:26pm
 
Not sure if this will help you, but I tried what antiplastik said, saving it with .java, but that messed up the rest of the code. It seems that Processing does not accept static variables unless you put it in its own static class. But that will work.
Re: static class variables
Reply #4 - Jan 11th, 2010, 6:21pm
 
actually it kinda worked. I didn't get your problem, but I got a different problem (and also I don't want a static class).

myClass uses Processing commands like "fill" which myClass now can not find.

A while back I played with using Processing in Eclipse, so I am a little familiar with what may fix this problem. Else I'll still take some hints.

Mike
Re: static class variables
Reply #5 - Jan 11th, 2010, 11:46pm
 
JM Davis wrote on Jan 11th, 2010, 6:21pm:
myClass uses Processing commands like "fill" which myClass now can not find.



That was what was happening for me as well. Using another static class, with a static method inside it, at least gave me a type of 'global' variable. I am not too familiar with the use of static variables myself, just sort of testing it out, so I would be curious to know how you are going to use it As I understand it, Processing does not allow the 'normal' use of static instance variables, and I would be curious to know why that is as well. Not that it is a big issue to me ...  Huh Roll Eyes
Re: static class variables
Reply #6 - Jan 12th, 2010, 1:23am
 
JM Davis wrote on Jan 11th, 2010, 6:21pm:
actually it kinda worked. I didn't get your problem, but I got a different problem (and also I don't want a static class).

myClass uses Processing commands like "fill" which myClass now can not find.

A while back I played with using Processing in Eclipse, so I am a little familiar with what may fix this problem. Else I'll still take some hints.

Mike


I'm sure I saw a question like this recently.  IIRC the suggested fix was to pass a reference to PApplet to your Java class when you instantiate it.  If I'm right then you should be able to reference this to access Processing methods and classes.

If that doesn't work I'm sure someone will correct me; otherwise you might want to look at the Libraries dev guidelines as it may be the sort of thing that crops up in that situation too...
Re: static class variables
Reply #7 - Jan 12th, 2010, 2:33am
 
JM Davis wrote on Jan 11th, 2010, 2:05pm:
globals!!! gack!)

Well, globals in Processing are actually members of the main class (which is hidden)... Smiley
If your static variables are final, ie. constants, they are, somehow, fine as globals, just lacking a bit of privacy... (ie. they can be seen by all classes).
For mutable statics, it is more annoying, but after all you have, by definition, only one value per class, so globals aren't so bad anyway, at worst prefix their names by the class name (yes, that's a bit ugly, though).
Or just go the Java way... loosing the facilities Processing offers (usage of functions instead of methods).
Re: static class variables
Reply #8 - Feb 14th, 2010, 10:29am
 
So, it's not possible to put static variable in non static class...
I think it's a real problem no ?
Re: static class variables
Reply #9 - Feb 14th, 2010, 10:34am
 
Not really.
Either you make the variable "global", not nice but usable, or you make a .java tab and you can do whatever you want there.
Re: static class variables
Reply #10 - Feb 14th, 2010, 11:34am
 
A PImage variable can't be "final" ...
Ok for the java tab but in that case no more processing's language is permitted : it's not a real good solution.

I really don't understand why this limitation is implemented.
Re: static class variables
Reply #11 - Feb 14th, 2010, 12:06pm
 
As usual I'm just repeating what others have posted - but IIRC the usual suggestion to allow access to processing methods etc. from within a 'java class' is simply to pass a reference to the applet to your class constructor:

Code:
// java class constructor:
PApplet parent;

myJavaClass(PApplet parent, [other parameters]) {
 this.parent = parent;
}

// class instantiation:

MyJavaClass thisInstance = new MyJavaClass(this, [other parameters]);


This comes up every so often so I'm sure a search will provide a more comprehensive explanation Wink
Re: static class variables
Reply #12 - Feb 14th, 2010, 1:29pm
 
... so ... I've to say : thanks ... as usual <|;-)


In complement and after testing :
- the java class must include
import processing.core.*;

- and the processing language is usable in your case with :
parent.println("Static is possible");
Re: static class variables
Reply #13 - Feb 15th, 2010, 12:54am
 
Quote:
A PImage variable can't be "final" ...

Probably, but I don't see how it relates to global variables.
Quote:
no more processing's language is permitted

It can, as blindfish shown, it is just a bit less fluid (have to prefix all function calls).
[quote]I really don't understand why this limitation is implemented./quote]
It is a Java limitation, with probably a good reason, not a Processing limitation made to annoy programmers... Smiley
Re: static class variables
Reply #14 - Jun 17th, 2010, 5:06am
 
Well, I had the same problem, but I found it easier (and in my case more logic) to create a static class, with all the static variables it should have, and extend it with my other class:

static class CanvasObject{
  // static define variaveis "da classe", significando que se esta variave for alterada, eh alterada em TODAS AS PICS E MOVS!
 public static int canvasH = 400;
 public static int canvasW = 600;
 public static int canvasX = 200;
 public static int canvasY = 25;
 
 CanvasObject(){
 }
}


class Img extends CanvasObject{

 
 //pos da imagem relativa ao canvas
 int picX;
 int picY;
 // tamanho da imagem
 int picH;
 int picW;

...
...
...


That even gives me the possibility of having static variables ACROSS multiple classes! Which I'm using thoroughly..

PS my first post! yay!  :D
Pages: 1 2