class checker
{
public checker()
{
System.out.println("In the constructor");
}
}
public class StringTesting {
static String string1;
static String string2;
public static void main(String[] args)
{
checker[] obj;
obj=new checker[10];
}
}
What can i do to call the constructor of all the 10 objects of class checker?obj=new checker[10] statement doesn't call the constructor i want to know why?
Your current code doesn't create any objects of type checker
- it just creates an array which is capable of holding references to objects of type checker
. Initially every element in the array has a value of null
. It's important to understand that the array element values aren't checker
objects - they're just references. Multiple elements could hold references to the same object, for example, just like multiple variables of type checker
could have values referring to the same object. (You can think of an array as a collection of variables.)
If you want to call the constructor, you need to do so explicitly. For example:
// Names fixed to be more conventional
Checker[] checkers = new Checker[10];
for (int i = 0; i < checkers.length; i++) {
checkers[i] = new Checker();
}
See more on this question at Stackoverflow