Why Animal constructor is not being called by the declaration of the array? LINE 12

Isn't 10 instances of Animal class is being created with this line?

package javaapplication;

class Animal
{
    Animal() { System.out.println("Animal Constructor");}
}

public class Main
{
    public static void main(String[] args)
    {        
        Animal[] animal_obj = new Animal[10];
    }
}

Recommended Answers

All 2 Replies

Isn't 10 instances of Animal class is being created with this line?

Nope, ten uninitialized reference variables of type Animal are being created.

Creating an array only creates the array object. There are no Animal objects in it yet.
You need to fill the array with Animal objects, element by element.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.