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 :

1

2

3

4

5

6

staticpublicvoidmain(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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

packagecom.topjavatutorial;

publicclassExampleDemo{

/*

* No compilation issue with following main method

* Running the class will give following error :

* Main method is not static in class com.topjavatutorial.ExampleDemo

*/

publicvoidmain(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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

packagecom.topjavatutorial;

publicclassExampleDemo{

/*

* No compilation issue with private main

* Running the class will give following error :

* Main method not found in class com.topjavatutorial.ExampleDemo

*/

privatestaticvoidmain(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”.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

packagecom.topjavatutorial;

publicclassExampleDemo{

/*

* No compilation issue with following main method

* Running the class will give following error :

* Main method must return a value of type void

*/

publicstaticintmain(String[]a){

System.out.println("Returning int from main");

return1;

}

}

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().