Solved

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

Posted on 2006-11-06
4
8,080 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
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mysql jsp example issue 32 74
Is there a simpler dropbox system? 10 44
Eclipse Java import and method not resolved 4 81
jmeter usage 4 38
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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:

733 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