File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot reference non-static error

 
Tempora Telora
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I feel lost right now.

I have trying to reference a method from main ... test()

i have test has public void test() { System.out.println("hello"); }

but when I do that it comes back with non-static error. Why do I have to use a static method?

Why do I have to use the static variables outside of main?

I know i have done this before where outside of main I have private int counter = 0;

then in main I do counter++ but it keeps barfing on me. Any ideas?
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main method is a static method. It can directly access only static members of the class.
[ May 03, 2007: Message edited by: Keith Lynn ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A very common problem, partially caused because so many sources, even the Java Tutorial, put too much coding into the main method. That is convenient because they can get more code into less space, but it isn't good object-oriented programming. The main method should have very little in it; if you get down to one statement, so much the better.
If your test method is in a class called HelloClass, try this:-You are actually creating an object there.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic