Articles

What is significance of main method in java?

What is significance of main method in java?

If you are a java aspirant then you must have seen public static void main(String[] args)  while writting java code.

 

Now, what is the significance of main() method in java or Why it is important to write main method?

 

main method is the entry point for any java program.

Whenever we run our java code then JVM searches for main method with correct type signature (public static void main(String[] args){ }) in the source file. If it doesn't find the main method in the java source file then:

  • either it will not execute at all, or
  • if main is written with wrong type signature, it will throw run time error - 'Error: Main method not found in class'

Lets understand both the cases with example.


Java code with wrong main type signature

Lets write java code with wrong type signature:

  1. First we will spell main as Main

public static void Main(String[] args) is not correct name: main is the correct method name that JVM will search for when you run below code and when it doesn't find correct main method it throws error as shown in output.    


public class TIHMainDemo {

	public static void Main(String[] args){
		System.out.println("Wrong main type signature");
	}
}

OUTPUT

Error: Main method not found in class TIHMainDemo, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

 

If you get Main method not found error ever then immediately check the spelling or type signature of your main() method and correct it.

2. Omit public or static or void in main method

 

Here we have ommited static keyword thus on running this java code we get below error as shown in output.


public class TIHMainDemo {

	public void main(String[] args){
		System.out.println("Wrong main type signature");
	}
}

OUTPUT

Error: Main method is not static in class TIHMainDemo, please define the main method as:
   public static void main(String[] args)

 

Write above code without public or void keyword and run. You will get the error main not found for them as well.


Java code without main method

We can write java class without any main method within it but the class cannot run and provide any output. For example, consider below code:


public class TIHMainDemo {

	public void print(){
		System.out.println("No main method in class");
	}
}

If you run the above code nothing will happen.

Because main() method is the entry point for any java program but here it doesn't find any entry point on executing.

For print() method to work we need to call this method from a separate class containing main or add main method to this class as shown below:


public class TIHMainDemo {

	public static void main(String[] args){
		print();//calling print method
	}
	
	public static void print(){
		System.out.println("Called from main method");
	}
}

OUTPUT

Called from main method

 

Notice that I have added static keyword to the print() method while calling from within main() method. If we don't add static keyword then compilation error would occur - 'Cannot make a static reference to the non-static method print() from the type TIHMainDemo'. Learn in depth about it here.


Thus we have seen that:

  • public static void main(String[] args) is entry point for java program and execution starts from within main method.
  • It is extremely important to adhere to correct type signature of main method.
  • We can write class without main method but to execute the code within it we need to make a call from separate class that has main method in it. Standalone it won't run on its own.

So far we learnt that main is the entry point and without it execution will not happen. Its correct. 

But this gives arise to favorite interview question:


Java Interview Questions
Java Error and Solution

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Sonu Pandit
I am editor in chief at tutorialsinhand.com responsible for managing, reviewing and sending articles/contents for final approval to get published. Connect with me@https://www.linkedin.com/in/sonu-pandit-a77b471ab/. Join write4us program & share your skill
Page Views :    Published Date : Aug 27,2020  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!