0

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?

10
  • 3
    It won't run, or it runs and displays incorrectly? Exactly what goes wrong? Commented Oct 1, 2013 at 23:50
  • 1
    There is one problem: You have not initialized the serial variable. Add the code this.serial = count; to the constructor. Commented Oct 1, 2013 at 23:51
  • 1
    And please, if it doesn't compile, paste the compiler error. Commented Oct 1, 2013 at 23:51
  • 1
    If it doesn't run, paste the exception report. Commented Oct 1, 2013 at 23:52
  • 1
    You never actually print anything. You just return the strings you'd like to print. Commented Oct 2, 2013 at 0:23

4 Answers 4

1

static is a context that belongs to the Class, non-static methods are executed under the object's context

printStatus(); is exactly the same for all the zombies while printZombie will depend on the object.

Usually static methods should be stand-alone, like math functions.

Math.divide(NumberA, NumberB)

Static attributes/methods can be called outside the class using the class itself as reference, like this:

Zombie.count, if you are inside the class you can use it as in your main method, but it may look confusing

Sign up to request clarification or add additional context in comments.

Comments

1

Static methods can be accessed without instantiating the class i.e Zombie.printStatus(); as opposed to the printZombie method which can only be accessed when you have created a new Zombie object.

When you say you cant get it to run, what do you mean? what's going wrong? I think it's running but you;re not directing the output to the console:

You should be doing this:

System.out.println(printStatus());

This is because printStatus() merely returns a String.

Comments

1

You're exactly right:

1) The difference between "static" and "non-static" is basically the difference between "class-wide" and "per object instance".

2) Your "count" goes from 0 (before creating the 1st zombie) to 3 (after creating the last).

3) All three objects reference the same static "count".

What I don't get is how "serial" is incrementing :( Did you leave anything out of your code sample?

Here's what I get from your code:

// Modified code
public class Zombie {
  String name;
  int serial = 0;
  static int count = 0;

  public Zombie(String name) {
System.out.println ("Zombie(" + name + "): serial=" + serial + ", count=" + count + "...");
    this.name = name;
    count++;
  }

  static String printStatus() {
System.out.println ("printStatus: count=" + count + "...");
    String status;
    if(count == 1) {
      status = (count + " zombie created so far");
    }
    else {
      status = (count + " zombies created so far");
    }
System.out.println ("  " + status);
    return status;
  }

  String printZombie() {
System.out.println ("printZombie: serial=" + serial + ", count=" + count + "...");
    String ident = (name + " is zombie " + serial);
System.out.println ("  " + ident);
    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();
  }
}

Corresponding output:

printStatus: count=0...
  0 zombies created so far
Zombie(Deb): serial=0, count=0...
printStatus: count=1...
  1 zombie created so far
Zombie(Andy): serial=0, count=1...
printStatus: count=2...
  2 zombies created so far
Zombie(Carl): serial=0, count=2...
printStatus: count=3...
  3 zombies created so far
printZombie: serial=0, count=3...
  Deb is zombie 0
printZombie: serial=0, count=3...
  Andy is zombie 0
printZombie: serial=0, count=3...
  Carl is zombie 0

Comments

1

Very simply, static variables only have one copy no matter how many times they're instantiated. Non-static variables are the opposite i.e there are as many copies as the number of times they instantiated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.