I'm doing a fairly simple program in order to learn the difference between static and non-static methods and variables, and I thought I understood it, but I can't get the following code to run:
public class Zombie {
String name;
int serial = 0;
static int count = 0;
public Zombie(String name) {
this.name = name;
count++;
}
static String printStatus() {
String status;
if(count == 1) {
status = (count + "zombie created so far");
}
else {
status = (count + "zombies created so far");
}
return status;
}
String printZombie() {
String ident = (name + " is zombie " + serial);
return ident;
}
public static void main(String[] args) {
printStatus();
Zombie z1 = new Zombie("Deb");
printStatus();
Zombie z2 = new Zombie("Andy");
printStatus();
Zombie z3 = new Zombie("Carl");
printStatus();
z1.printZombie();
z2.printZombie();
z3.printZombie();
}
}
It should have an output of:
0 zombies created so far
1 zombie created so far
2 zombies created so far
3 zombies created so far
Deb is zombie 0
Andy is zombie 1
Carl is zombie 2
But I can't get it to run. I assume the problem (at least one of them) is with the first method, but I can't figure it out. count is supposed to be static and the other two variables aren't, and printStatus is supposed to be static but printZombie is not. Can someone please explain this to me?