| Author |
Non-static method cannot be referenced from a static context
|
T Shaw
Greenhorn
Joined: Jul 29, 2012
Posts: 27
|
|
I have three bits of code that are finally beginning to fit together in a pathfinding function I'm working on. Most of the pathfinding method itself hasn't been written yet, but after a day of trying to figure out how array and object syntax work, I'm having one last problem with these three bits:
a class defining each individual tile on the grid I'm trying to find paths over
a class that creates an array from that class
And the main code that calls the pathfind method.
That last line is being flagged with the error:
"Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - non-static method pathfind(byte,byte,byte,byte[][],boolean[][],initalize.battle.pathfinding) cannot be referenced from a static context
at initalize.battle.InitalizeBattle.main(InitalizeBattle.java:51)"
I'm not sure why that is.
|
 |
jasonhu hu
Greenhorn
Joined: Aug 01, 2012
Posts: 6
|
|
hi,you should use the object name to invoke the method,not the class name .
the method is not static ,so you can not dircetly use by class name.
|
 |
jasonhu hu
Greenhorn
Joined: Aug 01, 2012
Posts: 6
|
|
try the below...
****************************************
map2 = map2.pathfind(x,y,move,pathhistory,deadtiles,map2);
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 10043
|
|
There are two kinds of methods in java - static and non-static.
A static method is one that can be called without there being any object. It doesn't rely on the state of something. For example, a square root function can be called and you can get the result if you pass it in a number. i.e. sqrt(25) would return 5.
a non-statis method is one that needs an object to have been created. For example, it makes no send to call a method getName() if there is no Dog object created. What, exactly, would you get the name of?
You are calling a non-static method (pathfinding()), but doing so with the general class name, not a specific object. Jasonhu's suggestion should work.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
 |
|
|
subject: Non-static method cannot be referenced from a static context
|
|
|