Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need to Instantiate an Object Array Twice?

Tags:

c#

I have created an Object array like this. But to assign value to object, I have to instantiate each object at every positions of the array? Why do I need this?

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 Object Class

 public class StageObject
    {
        public string StageId { get; set; }
        public DateTime FromTime { get; set; }
        public DateTime ToTime { get; set; }
    }
like image 854
Subin Jacob Avatar asked Mar 15 '13 04:03

Subin Jacob


People also ask

Can an array be initialized twice?

No. The array is not being created twice. It is being created once and then it is being populated.

What does it mean to instantiate an array?

Arrays can be instantiated by specifying a list of initial values. • Syntax: datatype [] arrayName = { value0, value1, … }; where valueN is an expression evaluating to the data type of the array and is the value to assign to the element at index N.

Do arrays need to be instantiated when declared?

In order to use the above-declared array variable, you need to instantiate it and then provide values for it. The array is instantiated using 'new'. The general syntax of instantiating is as follows: array_name = new data_type [size];

What is the correct syntax to instantiate an array?

To initialize or instantiate an array as we declare it, meaning we assign values as when we create the array, we can use the following shorthand syntax: int[] myArray = {13, 14, 15}; Or, you could generate a stream of values and assign it back to the array: int[] intArray = IntStream.


3 Answers

I have to instantiate each object at every positions of the array?

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

For C#

Arrays (C# Programming Guide) - MSDN

The default value of numeric array elements are set to zero, and reference elements are set to null.

(Since the question was originally tagged for java)

For JAVA

4.12.5. Initial Values of Variables

  • 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.
like image 174
Habib Avatar answered Nov 14 '22 18:11

Habib


Your array is an array of StageObject references. The StageObjects themselves don't exist yet. Essentially each entry in the array merely "points" to or "refers" to a StageObject.

Before you call new StageObject(), each array element is null, meaning it's referring to nothing.

like image 22
Jonathon Reinhart Avatar answered Nov 14 '22 17:11

Jonathon Reinhart


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

Why is it like this? Because an initially-empty array is often what you want -- and even if it isn't, unless your object only has a no-arg constructor, Java wouldn't even know how to construct each object.

like image 25
yshavit Avatar answered Nov 14 '22 16:11

yshavit