Why the main method is public static void in Java?

If you have started learning Java and wondering why we have to type public static void for the main method, we’ve got you covered. In this post, we will discuss the significance of using public static void in front of the main method.

In the main method, we use public static void because the JVM requires direct access to the main method so that it doesn’t have to create an instance of that class. Declaring the method as static makes the method a class member, that is it doesn’t belong to any object, it belongs to all instances, so it will allow JVM to access the method directly without even creating the instance. And access specifier public signify whether that method is exposed to the outside of the class or not. Lastly, the return type is to be specified as void as JVM is not expecting any value to be returned from the main method, just because it is not handling the return value.

If we deviate from this syntax and try to change the main method signature, JVM will not be able to run your main method, as it was not expecting any change. It politely tells you “please define the main method as: “public static void main(String[] args)”.

Nevertheless, let’s see what happens if we make changes to the main method signature with some examples.

Can we change return type of main() method?

If you change the return type of the main method from void to a primitive data type, your class will get compiled, however, we will get a runtime error and JVM will complain saying the “Main method not found”.

See also  Computing Factorial in Java

In this below example we are trying to return an integer by changing the return type of the main method.

package com.techstackjournal;

public class Demo {

	public static int main(String[] args) {

		System.out.println("This is a test");

		return 0;
	}

}

Output:

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

What we understand from this is that JVM fails to find the method itself if you change the return type from void to something else.

Can we declare the main method as private?

If you declare the main method as private, your class will compile as a normal method, but JVM will not be able to locate the main method, as the main method is not found with the expected signature.

In this below example we are trying to change the main method access modifier from public to private.

package com.techstackjournal;

public class Demo {

	private static void main(String[] args) {

		System.out.println("This is a test");

	}

}

Output:

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

As you can see, similar to the earlier example, changing the access modifier to private will lead to runtime error as JVM will not be able to locate the main method.

Can we declare the main() method as private or protected or with no access modifier?

If we declare the main() method as private or protected or with no access modifier, Java will not be able to find the main method in your class when you run your program.

See also  Integer methods getInteger vs parseInt vs valueOf with Examples

We’ve already looked at an example above on what happens if we change the access modifier from public to private. The same will be the fate if you replace the public with protected or no access modifier.

Can we declare the main() method as non-static?

If we declare the main() method as non-static, Java will explicitly complain at runtime saying “Error: Main method is not static in class”.

package com.techstackjournal;

public class Demo {

	public void main(String[] args) {

		System.out.println("This is a test");


	}

}

Output

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

Can we declare main method as final?

Yes, we can declare the main method as final and Java will not throw any error for doing that.

We usually declare any method as final to prevent someone from overriding that method in the subclass. So, declaring the main method as final is not changing the behaviour of the method. You will not get any compilation error and Java can recognize the main method during execution even though it is final.

package com.techstackjournal;

public class Demo {

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

		System.out.println("This is a test");

	}

}

Output:

This is a test

Can we change the order of public static void main in Java?

Yes, we can change the order of public static void main to static public void main. Doing this will not result in compilation or runtime errors. However, you cannot change the placement of return type and method name, they will have to be at the end of the method declaration, return type followed by method name.

package com.techstackjournal;

public class Demo {

	static public void main(String[] args) {

		System.out.println("This is a test");

	}

}

Output:

This is a test