Blog

Java Main Method

Java main method is the entry point of any java program. And its syntax always public static void main(String[] args)

You can only change the name of String array argument, for example you can change args to arguments.

public static void main(String[] arguments)

Also String array argument can be written as String[] args or String args[].

public static void main(String args[])

Lets go through each keyword definition below:

public

public is the access modifier of the main method. It should be public so that this method can get executed during runtime .

If you make main method as non-public then this method cant be executed.

Main method without public

You will get the below error when you try to run a java program without public

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

Code to reproduce the same

public class FirstProgram {

	 static void main(String args[])
	{
		
		System.out.println("Testing main method without public");
	}

}

static

static goes into JVM memory that’s why main method contains static because there is no object present when java run time starts so JVM can load the class into memory and it can call the main method.

You will try to get the below error when you run a main method without static keyword

Error: Main method is not static in class com.test.basics.FirstProgram, please define the main method as:
public static void main(String[] args)

Code to reproduce the same

public class FirstProgram {

	
	public void main(String args[])
	{
		
		System.out.println("Testing main method without static");
	}
	
	
}

void

In Java program every method is mandatory to have return type, if there is no return type then we need use void

No use of returning anything in main method

Once the main method finished executing java program terminates, to keep things simpler the java developers are not returning anything back to JVM because of no use

You will get the below error from compiler

Return type for the method is missing

public class FirstProgram {

	public static main(String args[])
	{
		
		System.out.println("Testing main method without void");
	}	
}

main

Its the name of the method and when you run a java program it always looks for main method

Without the method name you will get below error:

You will get below error from compiler

Syntax error on token “void”, Identifier expected after this token

String[] args

Main method accepts a single argument of type String array. This is also called as java command line arguments. 

Lets test the arguments in a java program in eclipse

First right click on your java program and click Run Configurations

It Whistle java Main program with arguments

Enter 1 2 3 in the program arguments like below

Click RUN so you will get the below results

package com.test.basics;

public class FirstProgram {
	
	public static void main (String args[])
	{
		for(String a:args)
		{
		System.out.println("Printing the values from arguments "+a);
		}
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *