Say the file Demo.java contains the source code as
public class Demo{ }
when this code is compiled because it successfully compiles as
$javac Demo.java
but when it runs like
$java Demo
then it shows an exceptional error
The main method not found in the Demo class, define the main method as: public static void main (String [] args)
therefore, the compiler is not responsible for checking whether the main() method exists or not. The JVM is responsible for this. The JVM checks the main() method with prottoype as
public static void main(Strings[] args)
- Why is the JVM looking for the
main() method? Is it possible to change the main() method to any other main_hero() method?
The JVM is instructed to find the main() method from within the JVM. Yes, it is possible to change the main() method to the main_hero() method, but inside the JVM you need to specify the main_hero() method.
JVM{ public static void main(String[] args) }
to
JVM{ public static void main_hero(String[] args) }
- Why publication?
The JVM is installed either on drive C or on D, so public is used to call from anywhere.
- Why static?
main() method is not associated with an object, therefore, without an existing object, the JVM must also call this method. main method has nothing to do with the object.
- Why void?
The JVM will call the main() method, but what can the JVM do with the return value if the main() method returns. So it means void .
bishwas pokharel
source share