Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (6.5k points)
edited by
What if the static modifier is removed from the signature of the main method?

1 Answer

0 votes
by (11.3k points)

If you don't add the 'static' modifier in your main method definition, the compilation of the program will go through without any issues but when you'll try to execute it, a "NoSuchMethodError" error will be thrown. 

Now, this happens because when you execute a JAVA program, the JVM needs to know the sequence of execution i.e. needs to have a driver code, and what to execute. Any method that is non-static hasn't been allocated memory by default, on compilation. If no memory has been allocated to the method, in the eyes of the JVM, it doesn't exist, despite the compilation. And when the JVM searches for the 'main' function to execute, it isn't able to find it. This is why you'll get such an error. 

Browse Categories

...