wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
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" ?

Kristian Andersson
Greenhorn

Joined: Apr 23, 2004
Posts: 7
I got a class in which I want to call three methods. The problem is as stated in the subject of this post. I got no clue whatsoever of what I should to to make it work. Anyway, heres the code:

Would anyone please tell me what to do?
And if you want to help out further, I would like some help with the method "reverseArrayOrder". The point with this one is, as the name suggests, to reverse the array order. I either need a way to copy the matrix that comes with the method-call, or I need to create an empty matrix with the same size and use this to temporarily store contents of the arrays while moving their contents around. Right? I've thought of using an ArrayList, but I can't come up with a way to make it work...
Greg T Robertson
Ranch Hand

Joined: Nov 18, 2003
Posts: 91
You are attempting to call the eraseMatrixValues from the static method "main". If you did

Reason is that you can't call "non static" methods from a static method. As an alternative you could make the methods all static.
As an aside classes are usually named with the first letter as a capital.
[ May 10, 2004: Message edited by: Greg T Robertson ]
Gary Down
Ranch Hand

Joined: Apr 09, 2004
Posts: 46
I take it you're fairly new to Java (as I am) so I don't want you to get ahead of what you are learning but this looks like a good problem for the "Collections" interfaces.
try reading
http://java.sun.com/docs/books/tutorial/collections/interfaces/index.html
Good Luck,
Gary.
Ben Cramphorn
Greenhorn

Joined: May 13, 2004
Posts: 17
I had this problem when I was working with an object earlier. I kept on getting this error popping up when I kept on trying to write and retrieve data from the class file instead of the object I had created from the class..

Maybe this is what youare doing??
Ben Baker
Greenhorn

Joined: May 13, 2004
Posts: 5
change to public void main(String []args)

insted..... it should run.... Dynamic non static methords can not be static...
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by I Dispise:
change to public void main(String []args)

insted..... it should run.... Dynamic non static methords can not be static...

Java requires that main() be declared static, so this is not an option. I think other people have given the correct answer, though.

Layne


Java API Documentation
The Java Tutorial
Aurelian Tutuianu
Ranch Hand

Joined: May 13, 2004
Posts: 86
On the other hand you should try to move all the code from main function into something else (for example public void run() {...}).
After you've done this, rewrite the main method:



an alternate solution in respect to change all methods into static.

hope will help.


http://javasign.blogspot.com/
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
I somewhat disagree with Greg's code:The dual use of the identifier and_kri is correct, but confusing. I guess class names and variable names occupy different name spaces, but I would write:


Mike Gershman
SCJP 1.4, SCWCD in process
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: "Non-static method cannot be referenced from a static context" ?