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 › processing and the static keyword
Page Index Toggle Pages: 1
processing and the static keyword (Read 462 times)
processing and the static keyword
Apr 27th, 2006, 7:46pm
 
Hello,

I don't quite get the way processing deals with the static keyword.

The following code for example produces an error, even though it's legal in java:

Quote:

// Compiler Message: This static variable declaration is invalid, because it is
//                   not final, but is enclosed in an inner class, "A", located at...
class A {
 static int i = 0;
}
Re: processing and the static keyword
Reply #1 - Apr 27th, 2006, 8:52pm
 
classes in other tabs are treated as inner classes, which cannot use static variables in java.

unless you specify .java at the end of the name when creating a new tab, in which case you'll get straight java code, but as a result of it no longer being an inner class, you won't have access to the standard functions unless you pass the PApplet object to the other code.
Re: processing and the static keyword
Reply #2 - May 4th, 2006, 7:42pm
 
Well, apparently you can define the nested class as "static", then you are allowed to have static fields in the class.

I have a base class that keeps a list of all the instances it creates:

static class Base {
 static ArrayList bases = new ArrayList();
 ...
 Base(color c, String label) {
   this.c = c;
   this.label = label;
   bases.add(this);
 }
...
}

This *seems* to work, but to be frank I was surprised it did!  I'll report back if I find out more.

Owen
Re: processing and the static keyword
Reply #3 - May 4th, 2006, 8:29pm
 
From http://mindprod.com/jgloss/nestedclasses.html

"Inner classes are not permitted to have static methods or fields. That is not quite true. They are allowed static final compile time constants, which are treated as if there were literals. Sorry I don't know why the restriction. Nobody I have asked knows why. This is probably the single most annoying fact about nested classes. If inner classes need statics, they have to get the outer class to hold them, or you have to use static nested classes or you have to inherit the static fields. Oddly, inner classes are permitted to extend classes that do have static methods and fields."

So basically, I think if you have a suite of classes you've created in tabs, and they need to contain static fields, you'll have to define that class as "static" and you'll need to to be very careful how they work with the rest of the sketchbook.  

For example, if instances of these classes use locally defined utilities in other parts of the project, those will have to be defined as static too, because the static inner classes cannot use instance fields or methods of the toplevel class.

I TOTALLY agree with the quoted author above: not allowing static fields in nested inner classes (non-static) is really a bitch.

With all that said, you can simply coordinate the inner class with non-static outer fields, but it lacks the eligance of static fields within the class.  So if you have a tab Agent, it can declaire a list of all agents allocated like this:

ArrayList agents = new ArrayList();
class Agent extends Base {
 ...;

 Agent(Edge edge, color c, int sz, String label, float speed) {
   super(c, label);
   ...
   agents.add(this);
 }
...
}

I've tried it and it works, but I don't love it.

Owen
Page Index Toggle Pages: 1