Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Non-static method cannot be referenced from a static context

 
T Shaw
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try the below...

****************************************
map2 = map2.pathfind(x,y,move,pathhistory,deadtiles,map2);
 
fred rosenberger
lowercase baba
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic