我在学Java,我试着练习我自己的代码,结果上面写着
Error:(7, 9) java: non-static method paymentCal(int,int,int) cannot be referenced from a static context
我该怎么解决这个问题??
public class Payments {
public void paymentCal(int minHrWg,int dayHr,int weekday){
int wkpayment = minHrWg*dayHr*weekday;
System.out.println("This week's: "+wkpayment);
}
public static void main(String[] args) {
paymentCal(10,4,5);
}
}
我听说“static”是指变量或方法,它不会被改变。 每当我在方法中放入不同的值时,方法不就会改变吗?
有两种解决方案:
paymentCal()
静态,即Public static void paymentCal(int minHrWg,int dayHr,int weekday)
或
您需要通过为类创建一个实例来调用paymentCal()方法。 这可以通过使用
Payments payments= new Payments();
payments.paymentCal(10,4,5);
您还可以将该方法设置为静态的,如
public static void paymentCal(int minHrWg,int dayHr,int weekday){
int wkpayment = minHrWg*dayHr*weekday;
System.out.println("This week's: "+wkpayment);
}