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; }
}
No. The array is not being created twice. It is being created once and then it is being populated.
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.
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];
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.
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.
Your array is an array of StageObject
references. The StageObject
s 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With