如何在Java中定义main(String [] args)而不会收到警告和错误?

use*_*861 6 java eclipse warnings program-entry-point

当我在Eclipse项目的默认包中创建一个新的main.java文件时,它会生成一个如下所示的main方法:

public static void main(String[] args)
{
}
Run Code Online (Sandbox Code Playgroud)

这立即引发了一个警告说This method has a constructor name.建议的解决方法是删除void:

public static main(String[] args)
{
}
Run Code Online (Sandbox Code Playgroud)

现在而不是警告,我得到一个错误:Illegal modifier for the constructor in type main; only public, protected & private are permitted.如果我删除了static,我的代码现在看起来像:

public main(String[] args)
{
}
Run Code Online (Sandbox Code Playgroud)

这一次,我仍然得到一个错误,但另一个错误说:

Error: Main method not found in class main, please define the main method as:
    public static void main(String[] args)
Run Code Online (Sandbox Code Playgroud)

Argggh!但这让我回到了我开始的地方.如何定义主方法以便我不会收到任何错误或警告?

我正在使用Eclipse Juno Service Release 2JavaSE-1.7.请注意,我对Java很新; 我来自C#背景.此外,这可能是一个重复的问题,但我找不到它.

Vin*_*ele 14

不要打电话给你的班级main,但是Main.

一般来说,坚持Java编码标准:用大写(Main而不是main)来启动类名,你不会遇到这些问题.


Lio*_*onC 5

如果你命名文件,main.java那么类也必须被命名main,这是违反标准的(类以大写字母开头)但是可能.在类中,与类相同的方法被视为构造函数.因此,为了解决您的问题并符合标准,请将您的类和文件重命名Main为大写"M"