This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HelloWorld { | |
public static void main(String arg[]) { | |
String name = arg[0]; | |
System.out.println("hello " + name); | |
} | |
} |
.
Why the word static is used in “public static void main(String arg[])” ?
Why the word static is used in “public static void main(String arg[])” ?
When we enter the command “java HelloWorld psg”. Internally this command is converted to HelloWorld.main(“psg”).
We are trying to invoke the main function using class name. We are trying to invoke the main function without creating an object of HelloWorld class. Due to this reason we have to declare the main function as static.
.
Why the word void is used in “public static void main(String arg[])” ?
Why the word void is used in “public static void main(String arg[])” ?
This means the main function does not return anything.
.
Why the word main is used in “public static void main(String arg[])” ?
Why the word main is used in “public static void main(String arg[])” ?
When we enter the command “java HelloWorld psg”. Internally this command is converted to HelloWorld.main(“psg”).
The JVM will definitely will look for a main function in HelloWorld class.
Hence the method name must be “main”.