Main method in java

In java, one might be wondering how to set a trigger point for execution  in an application. Answer would be you require to have a main method in your application. A main method signature is as follows

public static void main(String[] args)

What is this main method?

main method is considered as an entry point used by JVM to start execution in java application. It contains String array as arguments. The signature can be written in the following ways given below.

public static void main(String args[]) {}

public static void main(String[] args){}

public static void main(String… args){}

Can we write static public instead of public static in the main method?

Yes, it is possible to write modifiers in either way (public static or static public), but the convention is to use public static.

You can also use modifiers like final, synchronized and strictfp with the main method.


 

One of the most important question, which is asked in interviews many times, is that why main method is public,static and void?

Well, we use the main method in the programming but not every one tries to get  know the reason behind it, unless this is asked to you in interview.

So,we will look into them one by one.

Why main method is public?

Any method or variable if declared public can be accessible outside its class. In java, main method is public ,so it will be visible to all other classes even which are not part of its package. JVM classes will also be able to access it easily.

Why main method is static?

  1. As we know, static variables or methods can be accessed without creating the instance of the class. So, JVM also does not need to create the instance of the class,where main method resides, to call main method.
  2. Suppose, there are overloaded constructor present in the class i.e. SubNumber(int a) in our example below. If main method is not static, then JVM has to create the instance of the class. Then, there would be ambiguity which constructor should be called . If calling SubNumber(int a), then what value of ‘a’ needs to be passed.

Why main method is void?

main method is not supposed to return any value to JVM, so it has kept as void which means returns nothing.


 

Can a main method be overloaded?

Yes, we can have as many overloaded main method as we want. The no. of arguments should be different for overloaded method.

Look at the below code for better understanding. If you want to know more about what is overloaded method, you can read the later post.

public class SubNumber {
	
//overloaded main method 
	public static void main(int a){
		System.out.println("overloaded main method");
	}

	public  static  void main(String args) {				
		System.out.println( "name" );
	}
}

 

Ask Question
If you have any question, you can go to menu ‘Features -> Q&A forum-> Ask Question’.Select the desired category and post your question.
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...

1 Response

  1. August 16, 2015

    […] A new window will open, where you can give package name and class name. Check checkbox ‘public static void main(String[] args)’ and click finish. If you want to know about what is ‘public static void main(String[] args)’,Click here. […]