Reputation: 320
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?
Upvotes: 1
Views: 725
Reputation: 5940
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 Student
B
takes the reference returned by new
which reference the 10 blocks of Student
referencesnew
are initialized to null
Attempting 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 referenceUpvotes: 1
Reputation: 1615
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.
Upvotes: 0
Reputation: 20163
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:
Upvotes: 2
Reputation: 2009
in Student[] stu = new Student[10];
you initialize an array of 10 elements that take up 10xsize of the Student
object. But with stu = new Student();
it takes up only 1xsize of the Student
object.
Upvotes: 0
Reputation: 396
I 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.
Upvotes: 0
Reputation: 7326
In the array instantiation/initialization you are allocating enough memory to hold 10 Student objects. In your instantiation/initialization of stu, Your stu variable refers to one Student object in memory
Upvotes: 0
Reputation: 391
When you initialize an array, all the elements are null, initially. When you make a Student
object, it actually calls the Student
constructor.
Upvotes: 0