3

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

    }
}
Héctor
  • 24,444
  • 35
  • 132
  • 243
  • 2
    What do you mean? You can't create a main method but THE main method. It is the entry point in all Java applications. Welcome to SO, btw! – Héctor Aug 23 '18 at 10:56
  • As far as I am concerned the function/method shown is what is known as "the java main method" –  Aug 23 '18 at 10:58
  • JVM requires the main method to be included as it is. – The Scientific Method Aug 23 '18 at 10:58
  • You can use `String...` instead of `String[]`. And you don't have to call the parameter `args`. So it doesn't have to be _exactly_ like that. What are you actually after? – khelwood Aug 23 '18 at 10:59
  • 4
    Related: https://stackoverflow.com/questions/15173474/can-we-execute-a-java-program-without-a-main-method and https://stackoverflow.com/questions/39682655/how-to-configure-jvm-to-call-custom-method-instead-of-main-method and https://stackoverflow.com/questions/7443459/why-main-method-is-needed-in-java-main-class – Mark Rotteveel Aug 23 '18 at 11:00
  • What other way did you have in mind? – Kevin Anderson Aug 23 '18 at 11:02
  • @KevinAnderson I was thinking something like the c++ main method. –  Aug 23 '18 at 11:08
  • Java isn't C/C++, so why would you think that? Because of the C like syntax? – Kayaman Aug 23 '18 at 11:15
  • @Kayaman No, but rather because "int main()" is a lot shorter than Java's main method. Basically, I just wanted to know if there was a shorter main method. –  Aug 23 '18 at 11:49

4 Answers4

5

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Ashishkumar Singh
  • 3,580
  • 1
  • 23
  • 41
4

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.

vikingsteve
  • 38,481
  • 23
  • 112
  • 156
1

There was the hackish version removed in java7, leveraging static initializers. In java8 the initializer will still takeover, but needs an unused main method.

Not for actual use :)

public class Test {

   static {
     System.out.println("Hello world");
     System.exit(0);
   }

}
k5_
  • 5,450
  • 2
  • 19
  • 27
-1

You can create as many variations of main() methods you want like:-

int main(int i){...}
String main(){...}
etc....

But

 public static void main(String args[]) {...}  // JVM will call only this main method

This will be only considered as entry point to program.

Tarun
  • 986
  • 6
  • 19