2013-06-24 17 views
0

我仍然不完全理解我需要在項目的.java文件中使用「public static void main(String [] args)」標頭的位置。你需要把這個頭文件放在包的每個.java文件中嗎?將獨立的java文件轉換爲作爲項目包一部分的文件?

我一直在跟着我的書中的第3章,將下載的獨立書籍源文件拖放到我的項目包中,但是我的包中的一些.java文件不喜歡那樣「public static void main (String [] args)「語句,即使我的開啓和關閉花括號在正確的位置。下面是這些文件中的一個(ERROR(S)在代碼中的註釋描述)的例子:

public class Rectangle 
{ 
    public static void main(String[] args){ 
    private double length;//ERROR: illegal start of expression 
    private double width; 

    /** 
    * Constructor 
    */ 

    public Rectangle(double len, double w) 
    { 
     length = len; 
     width = w; 
    } 

    /** 
    * The setLength method accepts an argument 
    * that is stored in the length field. 
    */ 

    public void setLength(double len) 
    { 
     length = len; 
    } 

    /** 
    * The setWidth method accepts an argument 
    * that is stored in the width field. 
    */ 

    public void setWidth(double w) 
    { 
     width = w; 
    } 

    /** 
    * The set method accepts two arguments 
    * that are stored in the length and width 
    * fields. 
    */ 

    public void set(double len, double w) 
    { 
     length = len; 
     width = w; 
    } 

    /** 
    * The getLength method returns the value 
    * stored in the length field. 
    */ 

    public double getLength() 
    { 
     return length; 
    } 

    /** 
    * The getWidth method returns the value 
    * stored in the width field. 
    */ 

    public double getWidth() 
    { 
     return width; 
    } 

    /** 
    * The getArea method returns the value of the 
    * length field times the width field. 
    */ 

    public double getArea() 
    { 
     return length * width; 
    } 

}//end of: public static void main(String[] args) 

}//end of: public class Rectangle ERROR: class, interface, or enum expected 

的誤差(S)上來後,我加入了「公共靜態無效的主要(字串[] args )「到現有的Rectangle.java文件。任何想法爲什麼發生這種情況?

+0

不,你不應該。主頭必須只存在於包的一個類中,因爲編譯器只運行一個包含主語句的類。 –

回答

-2

發生這種情況是因爲您的程序包中不能包含多個main()方法。但是重申主要方法是絕對允許的。我的意思是,只要參數號碼不同,您可以覆蓋main()方法。

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

而在其他或同等級別的地方,你可以添加這樣的類

public static void main(String arg1, String arg2) { 
    ..... 
} 

ERROR: illegal start of expression是因爲你正在使用的方法內訪問修飾符。使用的所有接入mofifier和變量聲明的類

public class Rectangle { 

    private double length;//ERROR: illegal start of expression 
    private double width; 

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

ERROR: class, interface, or enum expected是因爲類Rectangle是唯一住房的靜態方法,你的所有的方法和參數稱爲靜態方法裏面裏面main()

Here是你的代碼,它會編譯沒有錯誤。

+0

爲什麼downvotes? – Starx

+0

謝謝。我也發現這一天: http://www.homeandlearn.co.uk/java/java.html 這似乎是一個非常好的來源初學者 java教程。 –

0
private double length; 

這個錯誤是因爲你不能有一個方法局部變量的訪問修飾符。訪問修飾符用於類變量。訪問級別修飾符確定其他類是否可以使用特定字段或調用特定方法。

在Java編程語言中,每個應用程序都必須包含一個main方法的簽名是:

但是,這並不意味着,在您的應用程序的每個類應包含一個主要方法。主要方法類似於C和C++的主要功能;它是您的應用程序的入口點,隨後將調用您的程序所需的所有其他方法。

我會建議您在跳入寫入程序之前理解Java應用程序,類,訪問修飾符的解剖結構,並讓自己完全困惑。這裏有一些鏈接,鏈接到幫助您:

http://docs.oracle.com/javase/tutorial/getStarted/application/#MAIN

http://docs.oracle.com/javase/tutorial/java/javaOO/index.html

http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

0

main是一個簡單的方法名。按照慣例,當您使用java <someclass>命令時,該命令在<someclass>中查找帶有特定簽名(一組參數)的main方法,並調用該方法作爲Java應用程序的入口點。

沒有規則表示在每個班級中都不能有main方法,但只有java命令行上命名的班級中的班級具有除了另一種方法之外的任何特殊意義。

也就是說,爲避免混淆,最好避免使用main作爲方法名稱,除非您打算將它作爲應用程序的「主入口點」。

您的錯誤僅僅是因爲您的語法無效 - 當您添加main時,您沒有輸入完整的方法。

相關問題