for example:
Student[] stu = new Student[10];
Student stu = new Student();
what's the differences between them?
and what happened when initializing an array of objects? why doesn't it call the constructor?
Initializing an array creates empty spaces for each element. It does not create any individual element. This
Student[] stus = new Student[10];
therefore creates ten empty spaces, into each of which can be placed a Student object. No other kind--unless it sub-classes Student. Each of these ten spaces are null until you explicitely place a new object into them. Such as with
stus[1] = new Student(); //1 is the *second* element
Initializing an object creates a new object of that type. This
Student stu = new Student();
creates a new Student object. Alternatively, this
Student stu;
declares the object, but does not create it. It's just like when you initialize the array. This allots a space for the (one) Student object, but does not create it.
To repeat, this creates (initializes) it--meaning places it in that declared space:
stu = new Student();
As a final note, as mentioned by @FlorentBayle, you should not name these objects the same. Consider naming the array as I do above: stus, which is more indicative of its actual value.
More information:
Given Student.java:
public class Student
{
private String name;
public Student () { name = ""; }
public String getName () { return name; }
public void setName (String name) { this.name = name; }
}
Then we have:
Student[] A = null;
A is a reference of type Student[] which means Array of Student referencesA has null value; meaning it refers to nothing
Student[] B = new Student[10];
B is a reference of type Student[]
new Student[10]; will reserve a memory of 10 memory references of type StudentB takes the reference returned by new which reference the 10 blocks of Student referencesnew are initialized to nullAttempting B[0].getName(); will compile, but will throw a NullPointerException on run-time
B[0] = new Student();
B[0] is the first element in the array B initially refers to null
new Student(); will create an Object of type Student by invoking its constructor Student() known as the default constructorAfter this, you can call Student functions like B[0].getName();
Student C = null;
C is a reference of type Student
C has the value of null which means it refers to nothingAttempting C.getName(); will compile, but will throw a NullPointerException on run-time
C = new Student();
C will take the reference returned by new Student();
new Student(); will create an object of type Student by calling its constructor Student() and return its memory referenceI agree with PakkuDon, don't know what you mean by "Structure Function."
Initializing an array of objects will make a new array of Student objects, which is basically a list of size Student[x] where x is the number of objects in the array.
When you actually add a Student object to the array, it will call the Student constructor, and create the Student object.
Simply creating a Student object will actually create the object, not just a list that can hold them.
Student[] stu = new Student[10]; creates an an array that can hold 10 Student objects. Each element is initialised to null.
Student stu = new Student(); creates a single Student.
As for your question on why the constructor isn't called when constructing an array of objects, I'd ask "Why would it be?" The compiler only knows that the array can store objects that are an instance of Student. How is the compiler supposed to know which constructor it is supposed to call to construct these objects? What should the compiler do in the event that your class, Student, lacks a no-args constructor?
There's also the fact that the programmer may wish to populate the array with data retrieved from an external source during runtime. It would make more sense to default to null rather than to populate the array with a bunch of arbitrary instances that are going to be discarded later since that'll add an unnecessary overhead to the program.
Structure function? Are you referring to the constructor?stu = new Student();will not work ifstuis the array ofStudentyou just declared the line before. If it's not, you should really choose another name.