This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Non-static method cannot be referenced from a static context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Non-static method cannot be referenced from a static context" Watch "Non-static method cannot be referenced from a static context" New topic
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: 11153
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
Don't get me started about those stupid light bulbs.
 
subject: Non-static method cannot be referenced from a static context
 
Similar Threads
Handling multi-dimensional arrays.
Mouse listener question
sonir, read pls
Vector help
Help in Algorithm Construction