java 不能从静态上下文中引用非静态方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10089826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
non static method cannot be referenced from static context
提问by seanysull
The code below occurs in the main class of a package I'm trying to create. It references objects and methods from a helper class called Journey. At the call of the journeyCost
method in the line marked by stars I get a "non static method cannot be referenced from static context" error. This has confused me as I was under the impression that the Journey object "thisJourney", created in the second line, constitutes an instance of the class and thus means the context is not static. Thanks in advance, Seany.
下面的代码出现在我试图创建的包的主类中。它从名为 Journey 的辅助类引用对象和方法。在journeyCost
用星星标记的行中调用方法时,我收到“无法从静态上下文中引用非静态方法”错误。这让我感到困惑,因为我的印象是,在第二行中创建的 Journey 对象“thisJourney”构成了类的一个实例,因此意味着上下文不是静态的。提前致谢,西尼。
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);
}
}
回答by talnicolas
The error means that you are trying to call a non static method in a static way, like maybe this one:
该错误意味着您正在尝试以静态方式调用非静态方法,例如:
Journey.journeyCost(thisJourney);
Is journeyCost()
declared static? Don't you mean instead thisJourney.journeyCost()
?
是否journeyCost()
声明为静态?你不是说代替thisJourney.journeyCost()
吗?
Plus you should use getters and setters to modify and access your member variables, so instead of:
另外,您应该使用 getter 和 setter 来修改和访问您的成员变量,而不是:
Journey.dayCharge = ...
you should have
你应该有
Journey.setDayCharge(Journey.getDayCharge() + thisJourney.getCostOfJourney());
(setDayCharge
and getDayCharge
need to be static in this case)
(setDayCharge
并且getDayCharge
需要在这种情况下,静态)
回答by Chandra Sekhar
change
改变
Journey.journeyCost(....)
to
到
thisJourny.journyCost(...........)
your journyCostis a non-static method of Journyclss, so you have to invoke this method through its object which is thisJourny
您journyCost是一个非静态方法JournyCLSS,所以你必须通过调用它的对象是这种方法thisJourny
using the class name, you can only access static membersor can invoke static methodsof that class.
使用类名,您只能访问静态成员或调用该类的静态方法。
回答by jzworkman
All of these lines needs to be changed. Unless you are really trying to change all future Journey Charges with your last three lines(and that would be assuming those are static values)
所有这些行都需要更改。除非您真的想用最后三行更改所有未来的旅程费用(并且假设这些是静态值)
thisJourney.costOfJourney = thisJourney.journeyCost();//dont know why you are passing the journey object to this method.
Journey.dayCharge = Journey.dayCharge + thisJourney.costOfJourney;
Journey.weekCharge = Journey.weekCharge + thisJourney.costOfJourney;
Journey.monthCharge = Journey.monthCharge + thisJourney.costOfJourney;
Those last three lines still need work, I dont know why you are trying to modify the static variable. Try this instead if you just want to set the charges of thisJourney
最后三行仍然需要工作,我不知道您为什么要修改静态变量。如果您只想设置费用,请尝试使用此方法thisJourney
thisJourney.dayCharge = Journey.dayCharge + thisJourney.costOfJourney;
thisJourney.weekCharge = Journey.weekCharge + thisJourney.costOfJourney;
thisJourney.monthCharge = Journey.monthCharge + thisJourney.costOfJourney;
Although even with that the charge values should be some constant. You really shouldnt be mixing a static class and instance class of the same type, while interchanging their uses.
尽管即使如此,电荷值也应该是恒定的。您真的不应该混合使用相同类型的静态类和实例类,同时互换它们的用途。
回答by Carlos Gavidia-Calderon
The method journeyCost
is non-static; so it's an instance method and it requires an instance of Journey
to get executed. The sentence Journey.journeyCost(thisJourney);
is invoking the method in a static way, and its expecting that your method is a class-level method (or static).
该方法journeyCost
是非静态的;所以它是一个实例方法,它需要一个实例Journey
来执行。这句话Journey.journeyCost(thisJourney);
是以静态方式调用方法,并且它期望您的方法是类级方法(或静态)。
So, you can make your journeyCost
method static for your call to work:
因此,您可以使您的journeyCost
方法静态化以便您的调用工作:
public static boolean journey(int date, int time, int busNumber, int journeyType)
Or try invoking the method from a proper instance:
或者尝试从适当的实例调用该方法:
Journey aJourneyInstance = new Journey();
thisJourney.costOfJourney = aJourneyInstance.journeyCost(thisJourney);
回答by Tom
Maybe the method journeyCost(Journey journey) should be static ?
也许方法 travelCost(Journey travel) 应该是静态的?
回答by CodeBlue
Whenener you use Journey.someMethod(), someMethod is a static method. "Journey" is in the static context. thisJourney is in the non-static context, because it is an instance. Therefore, you should use
当您使用 Journey.someMethod() 时,someMethod 是一个静态方法。“旅程”是在静态上下文中。thisJourney 在非静态上下文中,因为它是一个实例。因此,您应该使用
thisJourney.updateCurrentCharges(date);