Is the main method needed to write a java program?
This is my code:
package example;
public class HelloWorld {
public HelloWorld() {
}
public String getHelloWorld() {
return "Hello From Java!";
}
}
It shows an error at compilation:
java.lang.NoSuchMethodError: main
Exception in thread "main"
The main method is not needed in java programs. As others have pointed out, web applications do not use the main method.
It is not even needed in standalone applications. Consider
I compiled it and ran and obtained the following result:
For standalone applications you must either have
Main is preferred.