|
|
Re: static modifer in public static void main(Strin s[])
|
Posted: Dec 3, 2004 5:38 AM
|
|
First the basics (I don't know how much you know about the matter):
If you want to access a non static method in a class, you must first create an instance of that class: A static method can be called without creating an instance.
class MyClass {
public MyClass() {
}
public nonStaticMethod () {
}
public static staticMethod () {
}
}
class MyOtherClass {
public doSomething () {
//this won't work
MyClass.nonStaticMethod(); //in fact, it will produce a compile error
//these lines will work
MyClass.staticMethod();
MyClass c = new MyClass();
c.nonStaticMethod()
//this will work too
(new MyClass()).nonStaticMethod();
}
}
Now the answer to your question: If you call ">java -cp . MyStartClass", the engine won't create an instance of MyStartClass.
Therefore it can't access a non static method and that's why the main method has to be static.
|
|