This site requires JavaScript, please enable it in your browser!
Greenfoot back
darkmist255
darkmist255 wrote ...

2011/11/17

non-static method cannot be referenced from a static context

1
2
darkmist255 darkmist255

2011/11/17

#
I have several times had the error "non-static method cannot be referenced from a static context". This is the first time that it has really annoyed me. Why am I getting this error? Better yet, what does it really mean? Background: I am calling an actor class's method from the world class.
bourne bourne

2011/11/17

#
Where is it being called in the World class, and on what Actor exactly? What method and how are you calling it? static means something doesn't belong to a particular instance of some Object, but a Type of Object. So it is in the sense "global". These items then are called anywhere without needing an Object to call it upon. If it's not static, then you need an Object. Plus the method or field must be accessible. "private" does not give access to outside sources.
darkmist255 darkmist255

2011/11/17

#
Well it isn't private, so that shouldn't be a problem. The actor did not exist immediately, so maybe that was the problem?
bourne bourne

2011/11/17

#
What does it look like that you are trying to call?
kiarocks kiarocks

2011/11/17

#
I doubt it, post your code. I also have this problem when trying
Font[] f = GraphicsEnvironment.getAllFonts();
darkmist255 darkmist255

2011/11/17

#
I was playing around and for some reason it didn't save (o.O?), so I'll try to remember. I'll just use generic names, bear with me. I am calling actorName.methodName(parameter); and then in the actorName.class, I have something like: public void methodName(boolean wide) { if(wide = true) { setPosition(somethingX,somethingY); } }
bourne bourne

2011/11/17

#
Did the context within the World code that you were calling this have a reference to this Actor?
darkmist255 darkmist255

2011/11/17

#
Probably not as I don't understand references all that well. All I did was place the actor name (with a period) before the method within the actor. ex: Cat.Eat();
bourne bourne

2011/11/17

#
@kiarocks, getAllFonts() is not a static method of the class GraphicsEnvironment. So cannot be called upon the class, but an Object. This will work: GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment(); ArrayList<Font> fonts = new ArrayList<Font>(); Collections.addAll(fonts, e.getAllFonts()); // Note, getLocalGraphicsEnvironment() is a static method
bourne bourne

2011/11/17

#
@darkmist255, Basically was "Cat" declared somewhere within the World or the enclosing brackets of the same context?
darkmist255 darkmist255

2011/11/17

#
I don't believe it was. Was "GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();" declaring a reference?
bourne bourne

2011/11/17

#
Yes, that or a parameter of a method. The "e" then is accessible within the scope of where it is declared.
darkmist255 darkmist255

2011/11/17

#
So in general terms: ActorName variable = ActorName.ActorMethod(); variable will now be accessible for externally calling the method?
bourne bourne

2011/11/17

#
No. I'm a little confused by your question. But looks like you're trying to apply from "GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();" where GraphicsEnvironment (which is not an Actor) has a static method getLocalGraphicsEnvironment() that returns an Object that is of type GraphicsEnvironment, that which can be stored in a variable with that type. But having: "ActorType variable = new ActorType();" allows us to call "variable.someActorMethod()" within the scope of where we did the declaring
darkmist255 darkmist255

2011/11/17

#
Ahh, I see. So the "ActorType variable = new ActorType();" is our reference, variable is our variable (duh :D), and then when we do "variable.aMethod();", it calls on the method within that actor?
There are more replies on the next page.
1
2