Build a droid

this is the link for the project
https://www.codecademy.com/courses/learn-java/projects/build-a-droid

my question is I can not figure out the 13

here is my code:
public class Droid {
int batteryLevel;
String name;
public Droid(String droidName){
name=droidName;
batteryLevel=100;
}

public void energyReport(){
System.out.println(batteryLevel);
}

public void energyTransfer(Droid a,Droid b){
int exchange=a.batteryLevel;
a.batteryLevel=b.batteryLevel;
b.batteryLevel = exchange;
}

public void performTask(String task){
System.out.println(name+" is performing task: "+task);
batteryLevel=batteryLevel-10;
}
public String toString(){
return "Hey there, I’m the droid " + name;
}
public static void main(String args) {
Droid codey=new Droid(“Codey”);
codey.performTask(“dancing”);
System.out.println(codey.name);
Droid mandy=new Droid(“Mandy”);
System.out.println(mandy.name);
energyTransfer(codey,mandy);
System.out.println(codey.batteryLevel);
System.out.println(mandy.batteryLevel);

}
}

and the feedback is
Droid.java:32: error: non-static method energyTransfer(Droid,Droid) cannot be referenced from a static context
Droid.energyTransfer(codey,mandy);
^
1 error

Could someone please help me coreect my mistake?
I will be very grateful.

This needs to be a static method in order to invoke it like this:
energyTransfer(codey, mandy);
You can either add ‘static’ like so:
public static void energyTransfer(Droid a, Droid b)
or you can invoke the method like this:
codey.energyTransfer(codey, mandy); OR mandy.energyTransfer(codey, mandy);
The second option looks odd since both droids are passed as arguments, so I would go with the first one, but either will work.
Hope this helps && Happy coding!

2 Likes