Solved

Java error 'non-static variable numOfPass cannot be referenced from a static context'

Posted on 2007-03-25
7
1,930 Views
Last Modified: 2013-11-23
I am trying to write a java Elevator simulator with two classes, Class Elevator and Class Floor. I am trying to call a method in Class Floor from a method in class Elevator and I get the following errors:
E:\My Documents\Harvard\csci-e160\HomeWork\cscie160\hw2\Elevator.java:144: non-static variable numOfPass cannot be referenced from a static context
            Elevator.numOfPass = Elevator.numOfPass - Elevator.destReq[s];
                        ^
E:\My Documents\Harvard\csci-e160\HomeWork\cscie160\hw2\Elevator.java:144: non-static variable numOfPass cannot be referenced from a static context
            Elevator.numOfPass = Elevator.numOfPass - Elevator.destReq[s];
                                             ^
E:\My Documents\Harvard\csci-e160\HomeWork\cscie160\hw2\Elevator.java:144: non-static variable destReq cannot be referenced from a static context
            Elevator.numOfPass = Elevator.numOfPass - Elevator.destReq[s];
                                                                  ^
E:\My Documents\Harvard\csci-e160\HomeWork\cscie160\hw2\Elevator.java:145: non-static variable destReq cannot be referenced from a static context
            Elevator.destReq[s] = 0;

Here are my two methods:

      public void stop(int s)
      {
            System.out.println("\nStopping on floor " + curFloor);
            myFloors[curFloor].unloadPassengers(s);
      } // end of method stop

and

      public void unloadPassengers(int s)
      {
            Elevator.numOfPass = Elevator.numOfPass - Elevator.destReq[s];
            Elevator.destReq[s] = 0;
      }

Any ideas on how to resolve the errors so my code will compile?

Here is the whole code:

package cscie160.hw2;

public class Elevator
{
      int[] destReq = new int[8];  // add 1 to array size so current floor matches array elememt, for ex., curFloor 7 = int[7].
      Floor[] myFloors = new Floor[8]; // add 1 to array size so Floor[] = current floor.


      public static void main(String[] args)
      {
            Elevator myElevator = new Elevator();  // create new Elevator object

            myElevator.curFloor = BOTTOMFLOOR;
            myElevator.curDirectionUp = 1;
            myElevator.curDirectionDown = 2;
            myElevator.curDirection = myElevator.curDirectionUp;

            for (int i = 0; i < MAXELEVATORRUNS; i++)
            {
                  myElevator.boardPassenger(2);
                  myElevator.boardPassenger(2);
                  myElevator.boardPassenger(3);
                  myElevator.registerRequest(6, 6);


                  while (myElevator.curFloor <= TOPFLOOR && myElevator.curDirection == myElevator.curDirectionUp)   // create loop to move elevator up and down stopping only on requested floors
                  {
                        myElevator.move();
                  }

                  while (myElevator.curFloor >= BOTTOMFLOOR && myElevator.curDirection == myElevator.curDirectionDown)
                  {
                        myElevator.move();
                  }
            }  // end of for loop to board passengers and move the elevator
      } // end of method main


      public void move()
      {
            for (int s = 0; s <= 7; s++)  // for loop to determine if elevator should stop
            {
                  if (curFloor == s && destReq[s] > 0)
                  {
                        stop(s);
                  }
            }

            System.out.println(toString());

            if (curDirection == curDirectionUp)
            {
                  curFloor++;
            }

            if (curDirection == curDirectionDown)
            {
                  curFloor--;
            }

            if (curFloor == TOPFLOOR)
            {
                  curDirection = curDirectionDown;
            }

            if (curFloor == BOTTOMFLOOR)
            {
                  curDirection = curDirectionUp;
            }
      } // end of method move


      public void stop(int s)
      {
            System.out.println("\nStopping on floor " + curFloor);
            myFloors[curFloor].unloadPassengers(s);
      } // end of method stop


      public void boardPassenger(int floor)
      {
            destFloor = floor;

            for (int b = 0; b <= 7; b++)  // loop to enter requested floor in destReq array.
            {
                  if (floor == b)
                  {
                  destReq[destFloor] = destReq[destFloor] + 1;
                  numOfPass++;
                  }
            }
      } // end of method boardPassenger

      public void registerRequest(int floor, int passengers)
      {
            reqFloor = floor;
            reqPass = passengers;
            myFloors[reqFloor] = new Floor(reqPass);
      }


      public String toString()  // override toString method
      {
            StringBuffer status = new StringBuffer();
            status.append("Currently " + numOfPass + " passengers onboard. \nCurrent Floor: " + curFloor + "\n");

            return status.toString();
      } // end of method toString


      // declare values for constants

      private static final int CAPACITY = 10;
      private static final int BOTTOMFLOOR = 1;
      private static final int TOPFLOOR = 7;
      private static final int MAXELEVATORRUNS = 5;


      // declare instance values

      int curFloor;
      int curDirection;
      int curDirectionUp;
      int curDirectionDown;
      int destFloor;
      int numOfPass;
      int reqFloor;
      int reqPass;

} // end of class Elevator




class Floor
{
      public Floor(int passengers)
      {
            numOfWaitingPass = passengers;
      }

      public void unloadPassengers(int s)
      {
            Elevator.numOfPass = Elevator.numOfPass - Elevator.destReq[s];
            Elevator.destReq[s] = 0;
      }

      int numOfWaitingPass;
      int requestedFloor;
      int numOfUnloadingPass;


}
0
Comment
Question by:jhall13291
7 Comments
 
LVL 30

Expert Comment

by:mayankeagle
ID: 18788947
You have to create an object of Elevator and use it.

class Floor
{
      private Elevator e ;
      public Floor(int passengers)
      {
            e = new Elevator () ;
            numOfWaitingPass = passengers;
      }

      public void unloadPassengers(int s)
      {
            e.numOfPass = e.numOfPass - e.destReq[s];
            e.destReq[s] = 0;
      }

      int numOfWaitingPass;
      int requestedFloor;
      int numOfUnloadingPass;

}
 
0
 
LVL 30

Accepted Solution

by:
mayankeagle earned 500 total points
ID: 18788954
Better to have a method to set the numOfPass instead of doing it using the data member.

// in Elevator
public void setNumOfPass ( int numOfPass )
{
    this.numOfPass = numOfPass ;
}

public int getNumOfPass ()
{
  return numOfPass ;
}

// same way put get and set methods for all data members

// in Floor
e.setNumOfPass ( e.getNumOfPass () - e.getDestReq ()[s] ) ;
0
 

Author Comment

by:jhall13291
ID: 18789605
mayankeagle,

Thanks for the help. That cleared up the errors, but I guess my ultimate question is how do I pull the variable values from the Elevator class to the Floor class.

Also, I have already created an Elevator object and reference to that object in the Elevator class, e.g., 'Elevator myElevator = new Elevator()' Do I really need to construct a new elevator in the Floor class?

Thanks again for you help,
JHall
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 30

Expert Comment

by:mayankeagle
ID: 18789637
What is the relationship between the two?
0
 
LVL 16

Expert Comment

by:imladris
ID: 18793306
In the Elevator class you have:

 Floor[] myFloors = new Floor[8]; // add 1 to array size so Floor[] = current floor.

I would expect that to be followed by something like:

for(int i=0; i<7; ++i)myFloors[i]=new Floor(0,this);

The this variable should then be remembered in the Floor object so that it has a reference to the Elevator object for calling the Elevator objects methods and/or variables.


0
 
LVL 2

Expert Comment

by:jhshen
ID: 18793544
I think the method unloadPassengers(int s) is not suitable to be put in class Floor.
From the meaning of this method, it should be an action of Elevator.

My suggestion is:
1. remove the method unloadPassengers(int s) from Floor class
2. create a new method public void unloadPassengers(int currentFloor, int s) in Elevator class
Here is the part of sample code you may need to modify in class Elevator:
public void stop(int s)
      {
            System.out.println("\nStopping on floor " + curFloor);
            unloadPassengers(curFloor, s);
      } // end of method stop

      public void unloadPassengers(int currentFloor, int s)
      {
            this.numOfPass = this.numOfPass - this.destReq[s];
            this.destReq[s] = 0;
            //myFloors[currentFloor].numOfUnloadingPass = s;
      } // end of method unloadPassengers
0
 
LVL 16

Expert Comment

by:imladris
ID: 18808638
Did any of that help?

If so, it is time to select an answer and grade it.

If not, perhaps a clarifying question would help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now