为什么我会收到编译器错误,“非法启动表达式”?

时间:2013-07-12 13:58:48

标签: java

“非法启动表达式”错误与TestPayroll类中的这3个语句有关。提前致谢。

private String [] name = {"Tiny Tim", "Brad Pitt", "Madonna"};

private double [] payRate = {100.25, 150.50, 124.25};

private double [] hrsWorked = {40, 35, 36};

下面的TestPayroll.java:

/**
This program contains a Payroll class that has private data members for an employee's full name, hourly rate pay, number of hours worked, and total pay for the week. There is an array of 3 Payroll objects, which is instantiated manually with the number of hours each employee worked.  The program displays the amount of gross pay each employee has earned.  
*/

import javax.swing.JOptionPane;

public class TestPayroll {

public static void main(String[] args) {

    private String [] name = {"Tiny Tim", "Brad Pitt", "Madonna"};
    private double [] payRate = {100.25, 150.50, 124.25};
    private double [] hrsWorked = {40, 35, 36};

    //Payroll object
    Payroll calcPay = new Payroll(name, weeksPay);

    //Display weekly pay
    JOptionPane.showMessageDialog(null, "This week's pay for " + name[0].getName + " is " + payroll.getWeeksPay() +"\n" + "This week's pay for " + name[1].getName + " is " + payroll.getWeeksPay() + "\n" + "This week's pay for " + name[2].getName + " is " + payroll.getWeeksPay());

}   

}

public class Payroll {

public static void main(String[] args) {

    private String name;
    private double payRate;
    private double hrsWorked;
    private double weeksPay;

    //default constructor
    public Payroll() {
        this.name = name;
        this.payRate = payRate;
        this.hrsWorked = hrsWorked;
        this.weeksPay = weeksPay;
    }

    //Payroll constructor
    public Payroll(String name, double payRate, double hrsWorked, double weeksPay) {
        this.name = name;
        this.payRate = payRate;
        this.hrsWorked = hrsWorked;
        this.weeksPay = weeksPay;
    }

    //return name
    public String getName() {
        return name;
    }

    //set name
    public void setName(String name) {
        this.name = name;
    }

    //return pay rate
    public double getPayRate() {
        return payRate;
    }

    //set pay rate
    public void setPayRate(double payRate) {
        this.payRate = payRate;
    }

    //return hours worked for the week
    public double getHrsWorked() {
        return hrsWorked;
    }

    //set hours worked for the week
    public void setHrsWorked(double hrsWorked) {
        this.hrsWorked = hrsWorked;
    }

    //find week's pay
    public double getWeeksPay(double weeksPay) {
        double weeksPay = payRate * hrsWorked;
        return weeksPay;
    }   

}
}

6 个答案:

答案 0 :(得分:3)

public static void main(String[] args) {

是一个方法,而不是类定义。因此,它无法保存成员,这意味着将成员范围private添加到String name对编译器来说是无意义的。

现在,如果你有以下

public class Something {

  private String name;

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

}

这将是一个不同的故事。现在String name是类定义的一部分,而不是静态方法。由于它是类的成员,因此它可以具有类暴露范围

答案 1 :(得分:2)

这些属于不在方法中的类:

    private String [] name = {"Tiny Tim", "Brad Pitt", "Madonna"};
    private double [] payRate = {100.25, 150.50, 124.25};
    private double [] hrsWorked = {40, 35, 36};

或者您可以删除私有关键字,在这种情况下,它们将是main方法中的局部变量。

您的Payroll类的代码采用main方法。这不会编译。从main方法中删除您的代码并将其放入课程中。

答案 2 :(得分:1)

private String [] name = {"Tiny Tim", "Brad Pitt", "Madonna"};
private double [] payRate = {100.25, 150.50, 124.25};
private double [] hrsWorked = {40, 35, 36};

应该是,只是:

String [] name = {"Tiny Tim", "Brad Pitt", "Madonna"};
double [] payRate = {100.25, 150.50, 124.25};
double [] hrsWorked = {40, 35, 36};

删除private以使其成为局部变量;

(OR)

如果你想作为实例变量,直接在类中移动它们。

答案 3 :(得分:0)

删除这3行中每一行的private修饰符。或者,如果您认为它们是实例变量,请将它们移出main方法。这取决于你的意图。

答案 4 :(得分:0)

删除修饰符private

String [] name = {"Tiny Tim", "Brad Pitt", "Madonna"};
double [] payRate = {100.25, 150.50, 124.25};
double [] hrsWorked = {40, 35, 36};

它是局部变量的非法修饰符。局部变量在其声明之前只能有final个关键字。有关详情,请参阅JLS 14.4

答案 5 :(得分:0)

在方法中使用private修饰符是不合法的。

privatepublicprotected修饰符用于控制类之间的变量访问,而不是方法之间。在方法中声明的变量,只能在方法本身的范围内访问。

通过阅读Oracle官方Java教程中的Controlling Access to Members of a Class,您可以加深对变量修饰符的了解。