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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:34:14  来源:igfitidea点击:

non static method cannot be referenced from static context

javaoopstatic

提问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 journeyCostmethod 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());

(setDayChargeand getDayChargeneed 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 journeyCostis non-static; so it's an instance method and it requires an instance of Journeyto 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 journeyCostmethod 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);