In this article, we will discuss about the main method in Java and possible interview questions.
Java main method
In Java, main() method is the entry point for your application and will subsequently invoke all the other methods required by your program.
The main method accepts a single argument: an array of elements of type String.
public static void main(String[] args)
This array is the mechanism through which the runtime system passes information to your application. For example:
java MyApp arg1 arg2
Can “public static” in main() method signature be written as “static public” instead ?
The modifiers public and static can be written in either order (public static or static public), but the convention is to use public static.
We can also write it as static public as shown below :
static public void main(String[] args){ System.out.println("Hello"); }
Can we declare main method as non-static ? Why is it marked static ?
Main() being static, helps the compiler to invoke it in a class independent manner.
We won’t get a compilation issue by adding non-static main method as follows, but will give Runtime error.
package com.topjavatutorial; public class ExampleDemo { /* * No compilation issue with following main method * Running the class will give following error : * Main method is not static in class com.topjavatutorial.ExampleDemo */ public void main(String[] args){ System.out.println("Hello"); } }
Can we skip the argument(String[] args) for the main method ?
Removing the String[] args from main() method will result in Compiler treating it as any other normal method.
But your program needs a main method with String[] argument to run.
Can main method be private, protected or default ?
The compiler expects a main() method that is public.
You can define a private, protected or default main method. There won’t be any compilation issues. But if a class doesn’t have a main method that is specified with public access, running it as an application would produce Runtime error.
package com.topjavatutorial; public class ExampleDemo { /* * No compilation issue with private main * Running the class will give following error : * Main method not found in class com.topjavatutorial.ExampleDemo */ private static void main(String[] args) { System.out.println("Hello"); } }
Can the return type of main() method be other than void ?
The return type of main() is “int” in C and C++. But in Java, the main method return type should be “void”.
package com.topjavatutorial; public class ExampleDemo { /* * No compilation issue with following main method * Running the class will give following error : * Main method must return a value of type void */ public static int main(String[] a){ System.out.println("Returning int from main"); return 1; } }
If you change the return type of main to anything other than void, it will give a Runtime error “Main method must return a value of type void”.
Can main method call another main method ?
Yes, we can call main() of a class from another class using Classname.main().
Here is a puzzle on this concept :
Can argument of main method be anything other than String array ?
Argument of main method must be String[]. Passing an argument like int[] will result in a Runtime error.
package com.topjavatutorial; public class ExampleDemo { /* * No compilation issue with following main method * Running the class will give following error : * Main method not found in class com.topjavatutorial.ExampleDemo */ public static void main(int[] a){ System.out.println("Hello"); } }
Starting with JDK 5, main() method also accepts varargs of String type.
This is possible because the compiler internally replaces the vararg with a one dimensional array.
public class VarargsMain { //Note: main(String[] args) is replaced with main(String... args) public static void main(String... args) { System.out.println("Hello"); //prints Hello //String... args is internally replaced with String[] args by the compiler } }
You may read more about varargs and main method here.
What is the main method in Java and why is it important?
The main method in Java is the entry point for a Java application. It’s where the JVM starts executing the code and the program begins to run. The main method is important because it’s required to run a Java program.
Can you run a Java program without the main method?
No, a Java program cannot be executed without the main method. The JVM needs to know where to start executing the code, and the main method provides this information.
Can you overload the main method in Java?
Yes, you can overload the main method in Java. However, the JVM will only recognize the main method with the signature public static void main(String[] args) as the entry point for the program.
© 2016 – 2023, https:. All rights reserved. On republishing this post, you must provide link to original post