Print initialization messages from the constructor

I've got this exercise:

Create a class with a constructor that takes a String argument. During construction, print the argument.Create an array of object references to this class, but don't actually create objects to assign into the array. When you run the program, notice whether the initialization messages from the constructor calls are printed.

This is what I came up with:

publicclass Ex17{

Ex17(String str){

System.out.println(str);

}

publicstaticvoid main(String[] args){

Ex17[] array =new Ex17[5];

}

}

Ofcourse nothing happens when I run the program. How am I supposed to notice whether the initialization messages from the constructor calls are printed?

[1097 byte] By [brettosm8a] at [2007-11-26 23:45:56]
# 1

I guess the point is, if the constructor is called, you'd notice it, and it is not called,

you'd notice the absence of output. Does that make sense?

Some people mistakenly think this:

X [] v = new X[17];

actually does this:

X [] v = new X[17];

for(int i=0; i < v.length; ++i) {

v[i] = new X();

}

DrLaszloJamfa at 2007-7-11 15:18:36 > top of Java-index,Java Essentials,Java Programming...
# 2

To me, this means that the array is being initialized after its length is set.

X [] v = new X[17];

for(int i=0; i < v.length; ++i) {

v[i] = new X();

}

So are you saying that I'm not supposed to get any output.. The constructor wants a string not an object array?

brettosm8a at 2007-7-11 15:18:36 > top of Java-index,Java Essentials,Java Programming...
# 3

> I've got this exercise:

>

> Create a class with a constructor that takes a String

> argument. During construction, print the argument.

> Create an array of object references to this class,

> but don't actually create objects to assign into the

> array. When you run the program, notice whether the

> initialization messages from the constructor calls

> are printed.

>

> This is what I came up with:

>

> > public class Ex17 {

>

> Ex17(String str){

> System.out.println(str);

> }

>

> public static void main(String[] args) {

> Ex17[] array = new Ex17[5];

> }

> }

>

>

> Ofcourse nothing happens when I run the program. How

> am I supposed to notice whether the initialization

> messages from the constructor calls are printed?

That the whole point of this exercise. They want you to realize that the array of references are not initialized to object instances when you do this ClassName[] objarray = new ClassName[10]

as a lot of people think that the array of references are initialized to an array of instances when you do this ClassName[] objarray = new ClassName[10]

Message was edited by:

qUesT_foR_knOwLeDge

qUesT_foR_knOwLeDgea at 2007-7-11 15:18:36 > top of Java-index,Java Essentials,Java Programming...
# 4

This would have been a better demonstration of what goes on when you create an array:

public class X {

public X() {

System.out.println("X() called");

}

public static void main(String[] args) {

X[] v = new X[10];

//Why no output? Let's look further

for(int i=0; i < v.length; ++i) {

System.out.println(v[i]);

}

//Now I get it...

}

}

DrLaszloJamfa at 2007-7-11 15:18:36 > top of Java-index,Java Essentials,Java Programming...
# 5

Now it makes sense why this works:

public class Ex18 {

Ex18(String str){

System.out.println(str);

}

/**

* @param args

*/

public static void main(String[] args) {

Ex18[] array = new Ex18[5];

for (int i = 0; i < array.length; i++)

array[i] = new Ex18(Integer.toString(i));

}

}

brettosm8a at 2007-7-11 15:18:36 > top of Java-index,Java Essentials,Java Programming...