File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 10911
    
  12

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Non-static method cannot be referenced from a static context
 
Similar Threads
Vector help
Handling multi-dimensional arrays.
Help in Algorithm Construction
sonir, read pls
Mouse listener question