backspaces
Junior Member
Offline
Posts: 66
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