1

I've been studying from the Java Certification Bates and Sierra book and am stumped on chapter 2 constructor explanation:

public class Animal {
    String name;

    Animal(String name) {
        super();

        {System.out.println("Hello");} //I put this in myself

        this.name = name;
    }

    Animal() {
        this(makeRandomName());
    }


    static String makeRandomName() {
        int x = (int) (Math.random() * 5);
        String name = new String[] {"Fluffy", "Fido",
        "Rover", "Spike",
        "Gigi"}[x];
        return name;
    }

    public static void main (String [] args) {
        Animal a = new Animal();
        System.out.println(a.name);
        Animal b = new Animal("Zeus");
        System.out.println(b.name);
    }
}

The following is from the Bates and Sierra book:

Notice that the makeRandomName() method is marked static! That's because you cannot invoke an instance (in other words, nonstatic) method (or access an instance variable) until after the super constructor has run. And since the super constructor will be invoked from the constructor on line 3, rather than from the one on line 7, line 8 can use only a static method to generate the name.

I did an experiment and I inserted a super call in the overloaded constructor and my results were:

 Hello 
 Rover
 Hello
 Zeus

Now from these results, it seems as though the overloaded constructor AND the super constructor is executed before the static method because Hello prints before Zeus and Rover. So, why is there a need for a static variables?

What am I missing?

2
  • No, static will execute before the constructores, try putting a println in the makeRandomName() method and running it again Commented Oct 3, 2013 at 1:33
  • Just to clear a possible confusion, the fact that Hello prints before Zeus only tells you that Animal(String) is called before your System.out.println(), which is ovious in your main (keep in mind that the static method makeRandomName() does not print anything here). Commented Oct 3, 2013 at 12:29

3 Answers 3

3

The method makeRandomName() is called before super. Your print statements only demonstrate that you're consuming the value it produces after the other constructors execute. To see the difference, insert a print statement directly into makeRandomName().

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

Comments

1

The super() call in the constructor doesn't change anything. It would have been added implicitly by the compiler anyway.

As for execution

Animal a = new Animal();

calls the empty constructor, which calls

this(makeRandomName());

makeRandomName() is executed and returns a String which is used to call the 1-arg constructor that accepts a String. "Hello" is printed and the field name is assigned. The constructor returns. The random name gets printed.

Then the 1-arg constructor is called with

Animal b = new Animal("Zeus");

prints "Hello", sets the field, returns. Then the main prints "Zeus".


So, why is there a need for a static variables?

If you had removed static from the makeRandomName() method, the following would not compile

Main() {
    this(makeRandomName());
}

The object has yet to be initialized and therefore you can't call methods on it.

3 Comments

So just because the overloaded constructor gets called by this() doesn't mean its superconstructor gets executed? It only gets executed by Animal b = new Animal("Zeus");that would make sense to me. Am I getting it now?
@JaneKratz The super constructor is always called, either explicitly with some form of super(...) or implicitly by the compiler.
And I did insert a print statement in the makeRandomName() method and I see now where it prints before the overloaded constructor.
1

it seems as though the overloaded constructor AND the super constructor is executed before the static method because Hello prints before Zeus and Rover.

There is nothing in your code that proves that. Put a trace into the static method and you will see the real execution order.

So, why is there a need for a static variables?

There isn't. There are no static variables in the code you posted. There is a static method. Try making it non-static and you will see from the error that it can't be called from inside this() unless it is static.

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.