-1

The only clear commands of the main class for me are static, because you have to execute the method wihtout needing an object, void, because it has no return value and the String array name which you can decide by your own. But what I don't understand is why the access has to be public, why you can't decide the "main" name and why the parameter has to be a String array?

Are there other ways to create a method which is runnable like the casual main method?

This is not possible:

...
public static void mayne(Object zzz) {
...
}
...

We do not pass any arguments to the main methods parameter(s), why is a String array necessary?

Connor
  • 1
  • 1
  • 2
    The string array contains the command line parameters. See https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html – tgdavies Mar 26 '23 at 02:25
  • Does this answer your question? [Why is the Java main method static?](https://stackoverflow.com/questions/146576/why-is-the-java-main-method-static) – Karl Knechtel Mar 26 '23 at 02:26
  • 6
    [It is unclear what kind of an answer you want out of the "why" questions.](https://meta.stackoverflow.com/a/323382/5133585) Does "because the language specification says so" satisfy you? Please clarify. – Sweeper Mar 26 '23 at 02:28
  • Are you not talking [Runnable](https://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html)? Because OS? – Neil Mar 26 '23 at 02:37
  • You can write a Java class to be invoked by a runtime environment. For example you can write a [Jakarta Servlet](https://en.wikipedia.org/wiki/Jakarta_Servlet) to be invoked by a [Servlet container](https://en.wikipedia.org/wiki/Web_container) such as Tomcat, Jetty, Glassfish, etc. – Basil Bourque Mar 26 '23 at 05:31
  • Does this answer your question? [Can we execute a java program without a main() method?](https://stackoverflow.com/questions/15173474/can-we-execute-a-java-program-without-a-main-method) – Progman Mar 26 '23 at 10:10

1 Answers1

1

It seems you have two questions:

  1. Is there any other way to create a class runnable?

As of now, there is no other way this is how java is made where the main method is the entry point of running the java program. This you can understand as a constant identifier for the java- command to enter and run the java program.

  1. Why there is a string array in the main method?

There is a java command to run a java program. In this command, you can pass input for the main method which will then be passed as arguments in String array.

Example: java MyFirstJava a b abc 1

Here MyFirstJava is the class name This is a feature to get user input with command line.

Hope this helps.

Dourado
  • 110
  • 8
  • [Actually, it is technically possible without a `main` method](https://stackoverflow.com/a/15173828/5133585). – Sweeper Mar 26 '23 at 02:44
  • Yeah @Sweeper, technically adding static block can run java code before main method is executed but purpose is different for both static block and main method but as such there is no method or function available to act like main method which I understood from question asked. – Ashutosh Mishra Mar 26 '23 at 02:59