Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

861 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