1

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.

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

    } 
1
  • 1
    Please post (again...) the whole stack trace :P
    – m0skit0
    Apr 10, 2012 at 13:34

6 Answers 6

5

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()?

Plus you should use getters and setters to modify and access your member variables, so instead of:

Journey.dayCharge = ...

you should have

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

(setDayCharge and getDayCharge need to be static in this case)

3

change

Journey.journeyCost(....)

to

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

your journyCost is a non-static method of Journy clss, so you have to invoke this method through its object which is thisJourny

using the class name, you can only access static members or can invoke static methods of that class.

3

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.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.

1

Maybe the method journeyCost(Journey journey) should be static ?

1

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

    thisJourney.updateCurrentCharges(date);
1

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

So, you can make your journeyCost method static for your call to work:

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.