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

2017/1/6

non-static method cannot be referenced from a static context

BizCity BizCity

2017/1/6

#
I have a World (NexusBoard) and an Actor (Simple Hex). I have been fighting this error message for days, "non-static method ... cannot be referenced from a static context". For example I can not use "addObject(...)" (or World.addObject() or NexusBoard.addObject(...) or Greenfoot.addObject(),...) from "public void selectedClick( int myTurn) {...}" (NOTE: this is not specified as a Static routine (it looks just like "act()" in declaration. "act()" has to be an INSTANCE method (Non-Static). The word "static" appears NOWHERE in the Actor. "addObject(...)" works fine in the World (NexusBoard) unless I make the Method I am calling from "static" -- in which case I get this message on the "addObject(...)" call.
danpost danpost

2017/1/6

#
World is a class name. You cannot add an actor to a class. The compiler is looking for a 'static' method because you used the name of a class instead of an object reference. You need to add it to a World object. The Actor class method 'getWorld' returns the World object that the actor is in. Use:
1
getWorld().addObject(...)
You need to login to post a reply.