java静态非静态不同包之间调用_java – 非静态方法不能从静态上下文中引用

本文探讨了Java中非静态方法无法从静态上下文中被引用的问题。通过一个具体的示例,详细解释了如何正确地使用类的方法和变量,避免常见的编程陷阱。

参见英文答案 >

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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值