Open In App

Will the program run if we write static public void main in Java?

Last Updated : 20 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The entry point of a Java program is traditionally declared using the public static void main(String[] args) method. However, the language allows some flexibility in the order and visibility modifiers when declaring the main method. This has led to the question: What happens if we write static public void main instead of the conventional public static void main?

The Standard Declaration

The standard declaration of the main method in Java is:

Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    public static void main (String[] args) {
        System.out.println("GFG!");
    }
}


This syntax is established by convention and follows the rules specified by the Java language.

The Reordered Declaration

Surprisingly, Java is lenient when it comes to the order of access modifiers and the static keyword in the main method declaration. The following declaration is also valid:

Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    static public void main (String[] args) {
        System.out.println("GFG!");
    }
}


Here, we have switched the positions of static and public without any negative impact on the program’s execution. This flexibility might raise eyebrows among developers accustomed to strictly adhering to conventions, but it remains within the scope of Java’s language specifications.

Will the program run if we write static public void main in Java?

Yes, the Java program will run successfully if you write static public void main instead of the conventional public static void main. Java is flexible regarding the order of access modifiers, and both declarations are valid for the main method.

The Reasoning Behind It:

Java’s syntax flexibility in the ‘main’ method declaration is a result of the language’s design decisions. The main method is a special method that serves as the entry point for the Java Virtual Machine (JVM) when executing a Java program. The JVM looks specifically for the public static void main signature, but the order of public and static is not a concern.

This flexibility can be attributed to the design principle of making the language user-friendly and avoiding unnecessary constraints on developers. It allows programmers to write the main method in a way that aligns with their preferences while still adhering to the fundamental requirements.

Best Practices and Conventions

While Java allows the flexibility to reorder static and public in the main method declaration, developers are encouraged to follow established conventions. The standard declaration (public static void main) is widely adopted in the Java community, and deviating from it might lead to confusion among other developers who expect the conventional syntax.

Maintaining a consistent coding style across projects and teams fosters readability and collaboration. Adhering to conventions ensures that codebases are easily understandable and maintainable, even by developers unfamiliar with the specific project.

Conclusion:

In the grand scheme of Java programming, the order of static and public in the main method is a matter of personal or team preference. While the language allows flexibility, the best practice is to follow conventions for consistency and to make code more accessible to others. The orderly disorder in this aspect of Java syntax reminds us that, in a well-designed language, there’s often room for flexibility without sacrificing correctness or readability.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads