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 not allowing creation of new objects
Page Index Toggle Pages: 1
static class not allowing creation of new objects (Read 584 times)
static class not allowing creation of new objects
Jan 18th, 2009, 9:12am
 
Hi,
I have a static class that looks like this:

static class Steering {

 public static void steer(Fish obj, Vector desHeading) {
   Vector forwardVec = new Vector(cos(obj.heading), sin(obj.heading), 0);
   Vector sideVec = new Vector(forwardVec.y, -forwardVec.x, 0);
 }

}

obviously there's more, but that's the important part. I have another class called Vector that my non-static classes play nice with, but when I try to use it here I get "No enclosing instance of type SchoolingFish is accessible. Must qualify the allocation with an enclosing instance of type SchoolingFish (e.g. x.new A() where x is an instance of SchoolingFish)." SchoolingFish is the name of my sketch. Is it not possible to use non-compile-time classes in a static class?
Re: static class not allowing creation of new obje
Reply #1 - Jan 18th, 2009, 11:27am
 
I am not too sure of the exact problem, scope/visibility issues are subtle...
But I think the main issue is because all classes in a sketch (.pde file) are inner classes (of a class created at run time). Static classes here have a special meaning (and are not allowed for non-nested classes).
If you want to make an utility class made only of static methods, remove the "static" before "class", and put the class in a .java file: it will be external to the main class.
Re: static class not allowing creation of new obje
Reply #2 - Jan 18th, 2009, 11:37am
 
Well, that seemed to solve that problem, but now it's not recognizing the Fish class (which is a .pde file). It's looking like it might be easier just to rewrite my static class to not be static.
Re: static class not allowing creation of new obje
Reply #3 - Jan 18th, 2009, 1:13pm
 
The Fish class is probably internal to your main (invisible) class too. You might refer to it as SchoolingFish.Fish, I think (make it public if needed, although it might be the default in Processing).
Or just drop the static keyword, indeed... Smiley
Re: static class not allowing creation of new obje
Reply #4 - Jan 18th, 2009, 2:20pm
 
Thanks for the help. I ended up just dropping the static part and kind of faking it with something like:

Steering steering;

at the top of every class that needs to use it. Pretty much the same thing. Thanks again.
Page Index Toggle Pages: 1