Head First Java example explanation needed
<p>Hi! I'm reading Head First Java and i can't understand the behaviour of the following code:</p>

<pre><code>public class Dog {
    String name;
    public static void main(String[] args) {
        // make a Dog object and access it
        Dog dog1 = new Dog();
        dog1.bark();
        dog1.name = "Bart";

        // now make a Dog array
        Dog[] myDogs = new Dog[3];
        // and put some dogs in it
        myDogs[0] = new Dog();
        myDogs[1] = new Dog();
        myDogs[2] = dog1;

        // now acces the Dogs using the array references
        myDogs[0].name = "Fred";
        myDogs[1].name = "Marge";

        // Hmmm... what is MyDogs[2] name?
        System.out.print("last dog name is ");
        System.out.println(myDogs[2].name);

        // now loop through the array
        // and tell all dogs to bark
        int x = 0;
        while (x &lt; myDogs.length) {
            myDogs[x].bark();
            x = x + 1;
        }
    }

    public void bark() {
        System.out.println(name + " says Ruff!");
    }
</code></pre>

<p>The output produced by this code is the following:</p>

<pre><code>null says Ruff!
last dog name is Bart
Fred says Ruff!
Marge says Ruff!
Bart says Ruff!
</code></pre>

<p>I have a very hard time understanding this, IMO the code should run to somekind of infinite loop. From what i understand (and i've previously programmed in python): When the class is acticated, the main method gets called, and then inside the main method, two more classes of the same type are created. (Now here comes the incomprehensible part -->) When the new class is created, inside it's main method, 2 more classes are created and so on .. How can it be that it produces the output shown above, when it creates an infinte number of classes, so the code should actually never finish running. </p>

<p>Thank you!</p>
