【问题标题】:non static method cannot be referenced from static context [duplicate]不能从静态上下文引用非静态方法[重复]
【发布时间】:2023-03-31 00:54:01
【问题描述】:

下面的代码出现在我试图创建的包的主类中。它从一个名为 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);
        }

    } 

【问题讨论】:

  • 请发布(再次...)整个堆栈跟踪:P

标签: java oop static


【解决方案1】:

该错误意味着您正试图以静态方式调用非静态方法,例如:

 Journey.journeyCost(thisJourney);

journeyCost() 是否声明为静态的?你不是说thisJourney.journeyCost()吗?

另外,您应该使用 getter 和 setter 来修改和访问您的成员变量,而不是:

Journey.dayCharge = ...

你应该有

Journey.setDayCharge(Journey.getDayCharge() + thisJourney.getCostOfJourney());

(在这种情况下,setDayChargegetDayCharge 需要是静态的)

【讨论】:

    【解决方案2】:

    也许方法 JourneyCost(Journey Journey) 应该是静态的?

    【讨论】:

      【解决方案3】:

      当你使用 Journey.someMethod() 时,someMethod 是一个静态方法。 “旅程”是在静态环境中。 thisJourney 是在非静态上下文中,因为它是一个实例。因此,您应该使用

          thisJourney.updateCurrentCharges(date);
      

      【讨论】:

        【解决方案4】:

        改变

        Journey.journeyCost(....)
        

        thisJourny.journyCost(...........)
        

        您的 journyCostJourny clss 的非静态方法,因此您必须通过其对象调用此方法,即 thisJourny

        使用类名,您只能访问该类的静态成员或调用该类的静态方法

        【讨论】:

          【解决方案5】:

          所有这些行都需要更改。除非您真的想用最后三行更改所有未来的旅程费用(假设这些是静态值)

          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;
          

          最后三行仍然需要工作,我不知道您为什么要尝试修改静态变量。如果您只想设置 thisJourney 的费用,请尝试使用此方法

          thisJourney.dayCharge = Journey.dayCharge + thisJourney.costOfJourney;
          thisJourney.weekCharge = Journey.weekCharge + thisJourney.costOfJourney;
          thisJourney.monthCharge = Journey.monthCharge + thisJourney.costOfJourney;
          

          尽管如此,电荷值应该是恒定的。你真的不应该混合使用相同类型的静态类和实例类,同时互换它们的用途。

          【讨论】:

            【解决方案6】:

            方法journeyCost 是非静态的;所以它是一个实例方法,它需要一个Journey 的实例才能被执行。语句Journey.journeyCost(thisJourney); 是以静态方式调用方法,并期望您的方法是类级别的方法(或静态)。

            因此,您可以将您的 journeyCost 方法设为静态,以便调用工作:

            public static boolean journey(int date, int time, int busNumber, int journeyType)
            

            或者尝试从适当的实例调用方法:

            Journey aJourneyInstance = new Journey();
            thisJourney.costOfJourney = aJourneyInstance.journeyCost(thisJourney);
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2023-03-22
              • 2016-09-17
              • 1970-01-01
              • 1970-01-01
              • 2014-05-03
              • 1970-01-01
              相关资源
              最近更新 更多