Why do I need to instantiate an array of objects twice?

I created an array of objects like this. But in order to assign a value to an object, do I need to instantiate each object at each position of the array? Why do I need it?

This is my method

StageObject[] StageSplitDate = new StageObject[Stages.Rows.Count]; for (int i = 0; i < Stages.Rows.Count; i++) { StageSplitDate[i] = new StageObject(); StageSplitDate[i].StageId = "String Value"; StageSplitDate[i].FromTime = StartTime; StartTime =StartTime.AddMinutes(Convert.ToDouble(10)); StageSplitDate[i].ToTime = StartTime; } return StageSplitDate; 

And the class of the object

  public class StageObject { public string StageId { get; set; } public DateTime FromTime { get; set; } public DateTime ToTime { get; set; } } 
+6
source share
5 answers

Do I need to instantiate each object at each position of the array?

You do not create array elements twice. In the first line, you created an StageSplitDate array with each element set to null. By default, each element of the array (reference types) is initialized to zero. To use it further, you need to instantiate each object in the array as well, otherwise you will get a null link exception.

For C #

Arrays (C # Programming Guide) - MSDN

The default value for the numeric elements of the array is set to zero, and the support elements are set to null .

(Since the question was originally tagged for java)

For java

4.12.5. Initial Variable Values

  • Each class variable, instance variable, or array component is initialized with a default value when it is created (ยง15.9, ยง15.10):
  • For all reference types (ยง4.3), the default value is null .
+3
source

Your array is an array of StageObject references. StageObject itself StageObject not yet exist. Essentially, each record in the array simply "points" to or "refers" to StageObject .

Before you call new StageObject() , each element of the array will be null , which means that it says nothing.

+2
source

Think of an analogy when an array is a bookshelf. If you need a shelf of books, buying just a shelf is only the first step; you then need to buy each book and put it on the shelf. Same idea: allocating an array gives an empty container, and then you need to create each object and place it in the container.

Why is that? Since you often want an initially empty array - and even if it is not, if your object does not have a no-arg constructor, Java does not even know how to build each object.

+2
source

StageObject [] StageSplitDate = new StageObject [Stages.Rows.Count];

The above statement only makes a reference array for StageObject that are initialized to null but don't actually initialize StageObject

StageSplitDate [i] = new StageObject ();

The above statement creates an object of type StageObject and assigns a reference to the StageSplitDate element

+1
source

new StageObject[Stages.Rows.Count] creates a new array of StageObject references containing Stages.Rows.Count null references. You want each element to point to a StageObject . To do this, you need to create multiple instances of StageObject .

+1
source

All Articles