Solved

Cannot make a static reference to the non-static field...

Posted on 2006-11-06
4
8,088 Views
Last Modified: 2012-08-13
How do I stop getting the following error message:

'Cannot make a static reference to the non-static field Elevator.passengers'

__CODE__

package cscie160.hw2;


public class Elevator {
      int capacity = 10;
      int num_floors = 7;
      int passengers = 3;
      int current_floor = 0;
      static int cycle_count = 0;
      int print_floor = 0;
      boolean dir_up = true;
      int [] req_per_floor = {0,0,0,0,0,0,0};
      int [] pass_to_floor ={0,2,1,0,0,0,0};
/** Main Method
  * @param args
  *
*/                  
      public static void main(String[] args){
      Elevator myElevator = new Elevator();
                  
            
                  System.out.println(myElevator);            
                  myElevator.move();                  
            
      }
      
      
            //#4 move method
      public void move(){
            while (cycle_count <= 2){
            if(dir_up == true){
            current_floor = current_floor +1;
            stop();
            }
            else {
            current_floor = current_floor -1;
            stop();                  
            }
            if (current_floor == 0){dir_up = true;}
            if (current_floor == 6){dir_up = false;}
            if (current_floor == 6){cycle_count++;}
            }
      }
/**Overide the toString() from java.lang.Object
 * @return A string reporting the elevator's state at each floor.
*/  
      public String toString(){
            print_floor = current_floor + 1;
            return "Currently "+passengers +" passengers are onboard."+ "\n"+"Current floor :" + print_floor ;
      }
            
/** Stops the Elevator at each floor.  
 * @param print_floor
 * @return No return value
*/
   public void stop(){
               
         while (pass_to_floor[current_floor] >= 1){
               disembark_passenger();
         }
         while(req_per_floor[current_floor] >= 1){
               board_passenger();
         }
         print_floor = current_floor + 1;
      }
            
            
/** Board passanger method
 * @return No return value
*/
      public void board_passenger(){
            passengers = passengers + 1;
            req_per_floor[current_floor]--;
      }
/** Disembark passenger method
 * @return No return value
*/
      public void disembark_passenger(){
            passengers = passengers - 1;
            pass_to_floor[current_floor]--;
      }


}


public class Floor {
      
      

      int queued_passengers =0;
      
      public void BoardPassenger(){
            {
            if (Elevator.passengers <= Elevator.capacity){
                  queued_passengers = queued_passengers - 1;
                  Elevator.passengers++;
            }
            else{

            }
            return ;
            }
      }
}

__END__
0
Comment
Question by:chinamox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17885706
In class Floor you need to have created an instance or got a reference to Elevator
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 17885711
pass in a elevator instance

     public void BoardPassenger(Elevator elevator){
          {
          if (elevator.passengers <= elevator.capacity){
               queued_passengers = queued_passengers - 1;
               levator.passengers++;
          }
          else{

          }
          return ;
          }
     }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17888088
chinamox, why did you ignore my answer?
0
 

Author Comment

by:chinamox
ID: 17897350
CEHJ,  I found the code that objects posted more understandable, as it illistrated what I was doing wrong.  Thank you for your response though, if I had a way to divide my points I would have awarded you some as well.
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
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:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

690 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