Wednesday, January 12, 2022

main() method in java - detailed explanation

In today’s topic, we will discuss about the HelloWorld program which we have written earlier. 

class HelloWorld{ 

   public static void main(String[args){ 

System.out.println("Welcome Java Learning"); 

   } 

} 

For Compilation, javac HelloWorld 

For Execution, we give command java HelloWorld 

If there are static blocks, it first executes static blocks and then the main method. If there are no static blocks in your java class, then JVM looks for the main method in the class which we pass as an argument to this command because main() starting point of execution for any java program.  

 

 

 

As per the Java Language specification, JVM will always looks for the public static main method that takes String array as argument and void return type. 

Let us understand Why JVM looks for public static main() 

Why main method is static 

In general, methods of a java class will be called after an object is created to that class. But there is no object creation involved for the class that has main() as it will be called from JVM and JVM don’t try to create an object for the class you passed. In such cases, JVM knows only our class name. So it tries to call the main() with class name like below. 

HelloWorld.main(); 

As JVM calls the main() with class name, the main() should be declared with static. Static methods can be called with class name. 

Why main method is public 

Main method is called from JVM which is another program that is running outside of your application context or class. So for the main() to be available to any program running outside of your class, you have to declare that as public. 

Can we write static public instead of public static? 

Yes, we can write. Order of public and static is not considered. 

 

 

 

 

 

 

Can we modify the main() with return type other than void? 

main() in C and C++ return 0 for successful execution and non-zero value for failed execution. Those applications run as a process of operating system. So Operating system has to take of parent process based on the return value. 

main() in java should not return anything to anyone because it runs as main thread and main thread runs under JVM process. JVM can be terminated by System.exit(0); 

So main() in java should not return anything. 

If we write a main() with int data type, program will be compile but JVM still looks for main() with void return type. 

 

 

 

If we write one more main() with int data type, Compilation itself will fail saying class already has main(), because only with just return type difference we cannot have same method in same class in java 

 

 

 

Can we write one more method with new name myMain() but with same signature as that of main()? 

If we write one more method with new name myMain() and remaining all signature matches with main(), that will be allowed, compiled and executed. But myMain() will not be called from JVM, we have to call it explicitly and that will be executed only after JVM has executed the main(). 

 

 

 

 

 

 

 

Why main() takes string array argument as input? 

A method in java may or may not take an input that is as per requirement or specification. In the same way, java main method is designed to accept only the string array as argument to it. If you want to pass anything to main class via JVM, that has to be passed as string array 

 

 

 

 

args is just a variable name, you can use anything you want. 


  

 

No comments:

Post a Comment

Difference between Procedure Oriented Programming System and Object Oriented Programming System (POPS vs OOPS)

POPS vs OOPS   Today we will get to know the difference between Procedure oriented Programming system (POPS) and  Object-Oriented  Programmi...