参见英文答案 >
Non-static variable cannot be referenced from a static context 13个
下面的代码出现在我正在尝试创建的包的主类中.它从名为Journey的帮助程序类引用对象和方法.在星号标记的行中调用journeyCost方法时,我得到“非静态方法无法从静态上下文引用”错误.这使我感到困惑,因为我认为在第二行创建的Journey对象“thisJourney”构成了类的实例,因此意味着上下文不是静态的.谢谢,Seany.
public boolean journey(int date, int time, int busNumber, int journeyType){
Journey thisJourney = new Journey(date, time, busNumber, journeyType);
if (thisJourney.isInSequence(date, time) == false)
{
return false;
}
else
{
Journey.updateCurrentCharges(date);
thisJourney.costOfJourney = Journey.journeyCost(thisJourney);*****
Journey.dayCharge = Journey.dayCharge + thisJourney.costOfJourney;
Journey.weekCharge = Journey.weekCharge + thisJourney.costOfJourney;
Journey.monthCharge = Journey.monthCharge + thisJourney.costOfJourney;
Balance = Balance - thisJourney.costOfJourney;
jArray.add(thisJourney);
}
}
本文探讨了Java中非静态方法无法从静态上下文中被引用的问题。通过一个具体的示例,详细解释了如何正确地使用类的方法和变量,避免常见的编程陷阱。
1万+

被折叠的 条评论
为什么被折叠?



