简体   繁体   English

在java中,可以重命名或重构“public static void main”吗?

[英]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)? 我不想改变public static void ... String[] args签名的一部分,但是可以“重命名”这个函数(例如,只是为了好玩)?

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). 将它重命名为,例如, boot (如果不是历史的话,更好的反映在我的特定情况下它的实际用途)。


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? java中的public static void main(String arg [])是固定的吗?

Why the name main for function main() 为什么名称main为函数main()

No. The Java Language Specification says: 不, Java语言规范说:

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. Java虚拟机通过调用某个指定类的方法main来启动执行,并向其传递一个参数,该参数是一个字符串数组。

The JVM Specification says the same thing: JVM规范说同样的事情:

The Java virtual machine then links the initial class, initializes it, and invokes the public class method void main(String[]) . 然后,Java虚拟机链接初始类,初始化它,并调用公共类方法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. 简单的答案不,原因, 规范就是这样, JVM只会查找main而不是任何自定义名称作为起点。 It has to be called main with the exact signature public static void main(String[] args) 它必须被称为main,具有确切的签名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. 逻辑上也是有道理的JVM将如何知道而不是main方法,它必须寻找boot或其他东西,除非java命令有一个选项来传递start方法。

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. 其次,因为它标准化它也有助于开发人员社区,无论谁看到代码都知道如何运行给定的Java Standalone程序,或者说如果你有一个项目,你的第一点总是寻找主要方法,并从那里你继续前进。

No. You can not do that according to Java Language Specification. 不可以。根据Java语言规范,你不能这样做。 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). 但是如果你想要,因为Java是一个开源项目,所以下载完整的Java语言源代码并相应地进行更改(我的意思是更改JVM本身的源代码)。 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. 在开始时,JVM从方法public static void main查找,其中Strings数组作为参数。 So only thing you can do is rename argument args . 所以你唯一能做的就是重命名参数args If you want method like boot nobody stops you from doing something like this (personally I don't recommend that "pattern") 如果你想要像boot这样的方法,那么没有人阻止你做这样的事情(我个人不推荐“模式”)

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) . 您的应用程序从public static void main(String[] args)开始运行。 It's like the point where JVM looks at to start the proceedings. 这就像JVM开始审议的那一点。 If you change it, JVM will feel free to not start your application. 如果您更改它,JVM将随时无法启动您的应用程序。

If you want to want to have boot() as the starting point of your application - call it in main() . 如果你想将boot()作为应用程序的起点 - 在main()调用它。

Simple answer is NO . 简单的答案是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. 当你开始执行程序时,它会查找public static void main(String[] args) String数组参数的public static void main(String[] args)从这个入口点开始主线程

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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