• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"Non-static method cannot be referenced from a static context" ?

 
Kristian Andersson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
change to public void main(String []args)

insted..... it should run.... Dynamic non static methords can not be static...
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Aurelian Tutuianu
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic