當我在Eclipse項目,在默認包的新main.java文件,它會生成一個main
方法,看起來像:如何在Java中定義main(String [] args)而不會收到警告和錯誤?
public static void main(String[] args)
{
}
這立即引發寫着This method has a constructor name
警告。建議的解決方法是刪除void
:
public static main(String[] args)
{
}
現在,而不是一個警告,我得到一個錯誤:Illegal modifier for the constructor in type main; only public, protected & private are permitted
。如果我刪除static
,我的代碼現在看起來像:
public main(String[] args)
{
}
這一次,我還是得到一個錯誤,但不同的一個說:
Error: Main method not found in class main, please define the main method as:
public static void main(String[] args)
Argggh!但是,這使我馬上回到我開始的地方。我如何定義主要方法以便我不會收到任何錯誤或警告?
我正在使用Eclipse Juno Service Release 2和JavaSE-1.7。請注意,我對Java很陌生,我來自C#背景。此外,這可能是一個重複的問題,但我找不到它。
是您的類叫'main'以及? – 2013-04-26 15:33:48
你不應該使用'main'這樣的關鍵字作爲你的類的名字。 – 2013-04-26 15:36:15
main不是關鍵字。它只是一個約定 – tgkprog 2013-04-26 17:27:43