Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the Java "public static void main(string[] args)" the only way to create a main method? [closed]

Tags:

java

I want to know if the "Java main method" is the only way to create a main method in java.

The Java main method:

public class Test {
    public static void main(String args[]) {

    //example code

    }
}

2 Answers

JVM requires an entry point to start the execution and this entry point is defined as below in JVM

public static void main(String[] args)

So to answer your question, you can define a main method with any access modifier or with/without static keyword, but then it is not a valid main method, as the main method which the JVM uses as an entry-point should be defined as such.

like image 123
Ashishkumar Singh Avatar answered Dec 30 '25 13:12

Ashishkumar Singh


Equivalenty, yes, but syntactically - NO!

All of these are valid:

public static void main(String[] args)

public static void main(String[] foo)

public static void main(String... args)

Notice they are all an effectively equivalent method signature.

edit: one more -

public static void main(String args[])

edit: for interest's sake, final is implicit but can be added

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

Final note: even though variations are valid, it's usually best to stick with convention and go with the default.

like image 44
vikingsteve Avatar answered Dec 30 '25 13:12

vikingsteve



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!