My droid


#1

I'm getting this error where it says non-static method cannot be referenced from a static context. (It's three seperate errors all the same thing though. Heres my code any help would be great

public class Droid{
  int batteryLevel;

public Droid(){
  batteryLevel=100;
}
  public void activate(){
    System.out.println("Activated how can I help you");
    batteryLevel=batteryLevel-5;
        System.out.println("Battery level is" + batteryLevel + "percent" );
  }
  public void chargeBattery(int hours){
    System.out.println("Droid is charging...");
    batteryLevel=batteryLevel+hours;
    if(batteryLevel>100){
      batteryLevel=100;
                System.out.println("Battery level is" + batteryLevel + "percent" );
    }
    else{
              System.out.println("Battery level is" + batteryLevel + "percent" );
    }
  }
  public int checkBatterLevel(){
            System.out.println("Battery level is" + batteryLevel + "percent" );
    return batteryLevel;
    
  }
  public  void hover(int feet){
    if(feet>2){
      System.out.println("error i cannot hover above 2 feet");
      
    }
    else{
      System.out.println("Hovering...");
      batteryLevel=batteryLevel-20;
              System.out.println("Battery level is" + batteryLevel + "percent" );
    }
  }
public static void main(String[] args){

  
  Droid KY13 = new Droid();
  Droid.activate();
  Droid.chargeBattery(5);
  Droid.hover(1);
}
}

#2

Static methods are not associated with an instance of that class.
Non-static methods can only be called on instances of that class.

So without an object of that class, a non-static method can't be called.


#3

public static void main(String[] args){

Droid KY13 = new Droid();
Droid.activate();
Droid.chargeBattery(5);
Droid.hover(1);
}
}

Change to
public static void main(String[] args){

Droid KY13 = new Droid();
KY13.activate();
KY13.chargeBattery(5);
KY13.hover(1);
}
}
you called your Droid KY13 but weren't calling for it.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.