简体   繁体   中英

In java, can “public static void main” be renamed or refactored?

I do not want to change the public static void ... String[] args part of the signature, but is it possible to "rename" this function (eg. just for fun)?

So the entry point for execution would be a function with another name.

Rename it to, like, boot (what would better reflect, if not being historical, the actual use for it in my particular case).


related

I'm interested in doing something different, but these questions are still interesting:

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

Why the name main for function main()

No. The Java Language Specification says:

A Java virtual machine starts execution by invoking the method main of some specified class, passing it a single argument, which is an array of strings.

The JVM Specification says the same thing:

The Java virtual machine then links the initial class, initializes it, and invokes the public class method void main(String[]) .

Simple Answer No, Reason, Specification is like that, JVM will only look for main and not any custom name as the starting point. It has to be called main with the exact signature public static void main(String[] args)

Logically also it makes sense how will JVM know that instead of main method, it has to look for boot or something else unless java command had an option to pass the start method.

But that is asking just too much for no good reason.

Secondly since its standardized it helps the developer community too, whoever looks at the code know how to run a given Java Standalone program, or say if you have got a project, your first point will always be look for the main method, and from there you move on.

No. You can not do that according to Java Language Specification. But if you want, as Java is a open source project, so download the complete source code of Java language and change it accordingly ( I mean change the source code of JVM itself). This is the only way you can do that.

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

At start JVM is looking from method public static void main with array of Strings as argument. So only thing you can do is rename argument args . If you want method like boot nobody stops you from doing something like this (personally I don't recommend that "pattern")

static void boot(String[] arguments){
    //your logic
}

public static void main(String[] args) {
    boot(args);
}

Your application starts running from public static void main(String[] args) . It's like the point where JVM looks at to start the proceedings. If you change it, JVM will feel free to not start your application.

If you want to want to have boot() as the starting point of your application - call it in main() .

Simple answer is NO . When you start executing program it looks for public static void main(String[] args) which takes String array argument.From this entry point main thread get started.

是的,如果我们可以更改JVM的配置并让它查找具有其他名称而不是main方法的方法,我们可以更改main方法的名称。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM