In java, is it possible to rename or reorganize "public static void main"?

I don’t want to change the signature part of public static void ... String[] args , but is it possible to “rename” this function (for example, just for fun)?

Thus, the entry point for execution will be a function with a different name.

Rename it, for example, boot (which is better to reflect, if not historical, the actual use for it in my particular case).


related

I am interested in doing something else, but these questions are still interesting:

public static void main (String arg []) in java fixed?

Why main name for main () function

+6
source share
6 answers

Not. Java Language Specification says:

The Java virtual machine starts execution by calling the main method of a particular class, passing it one argument, which is an array of strings.

The JVM specification says the same thing:

The Java virtual machine then binds the source class, initializes it, and calls the public class void main(String[]) method.

+9
source

The simple answer is No, the reason the specification is this, the JVM will only look for main , and not for any user name, as a starting point. It should be called primary with exact signature public static void main(String[] args)

Logically, it’s also understandable how the JVM knows that instead of the main method, it should look for boot or something else, unless the java command could pass the start method.

But this is just too much, because there is no good reason.

Secondly, since its standardization helps the community of developers, those who look at the code know how to run this standalone java program, or say if you have a project, your first item will always look for the main method, and from there you move on .

+4
source

Not. You cannot do taht according to the Java Language Specification. But if you want, since Java is an open source project, download the full Java source code and modify it accordingly (I mean changing the JVM source code). This is the only way to do this.

So, now you can understand why I said that this is impossible.

+2
source

At startup, the JVM looks for the public static void main method with an array of strings as an argument. So the only thing you can do is rename the args argument. If you want a method like boot not to stop anyone from doing something like this (I personally do not recommend this "template")

 static void boot(String[] arguments){ //your logic } public static void main(String[] args) { boot(args); } 
+2
source

Your application starts with public static void main(String[] args) . This seems like the point where the JVM is trying to start the proceedings. If you change it, the JVM is not shy about starting your application.

If you want boot() to be the starting point of the application, call it in main() .

+1
source

The simple answer is NO . When you start the program, it looks for public static void main(String[] args) , which takes an argument from the String array. From this entry point, the main thread starts.

+1
source

Source: https://habr.com/ru/post/917617/


All Articles