Why main() method is Static
- the main function is where a program starts execution. It is responsible for the high-level organization of the program’s functionality, and typically has access to the command arguments given to the program when it was executed.
- main() is static because at that point in the application’s lifecycle, the application stack is procedural in nature due to there being no objects yet instantiated.It’s a clean slate. Your application is running at this point, even without any objects being declared (remember, there’s procedural AND OO coding patterns). You, as the developer, turn the application into an object-oriented solution by creating instances of your objects and depending upon the code compiled within.
- static allows main() to be called before an object of the class has been created. This is neccesary because main() is called by the JVM before any objects are made. Since it is static it can be directly invoked via the class.
- The method is static because there would be ambiguity: which constructor should be called? Especially if your class looks like this:
public class JavaClass { protected JavaClass(int x){} public void main(String[] args) { } }
If the JVM call new JavaClass(int)? What should it pass for x?
What would be the Output
public class Test { static { System.out.println("Hi static 1"); } public static void main(String[] args) { System.out.println("Hi main"); } static { System.out.println("Hi static 2"); } }
Output
Hi static 1 Hi static 2 Hi main
What happens when you run a class without main() method
Say, file Demo.java contains source code as
public class Demo{ }
when this code is compiled as it’s compile successfully as
$javac Demo.java
but when it’s executed as
$java Demo
then it shows an exceptional error
Main method not found in class Demo, please define the main method as: public static void main(String[] args)
so compiler is not responsible to check whether main() method is present or not. JVM is responsible for it. JVM check for main() method with prototype as
public static void main(Strings[] args)
Why JVM search main() method? Is it possible to change main() method into any other method main_hero()?
JVM is instructed to find main() method from inside JVM. Yes, it’s possible to change main() method into main_hero() method but inside JVM you must have to instruct to search main_hero() method.
Why public?
call from anywhere public is used.
Why void?
JVM is going to call main() method but what can JVM do with return value if main() method return.
Can we declare main() method as private or protected or with no access modifier?
No, main() method must be public. You can’t define main() method as private or protected or with no access modifier. This is because to make the main() method accessible to JVM. If you define main() method other than public, compilation will be successful but you will get run time error as no main method found.
Can We Declare main() Method As non-static?
No, main() method must be declared as static so that JVM can call main() method without instantiating it’s class. If you remove ‘static’ from main() method signature, compilation will be successful but program fails at run time.
Most of the Errors incase of change of main() method are runtime errors