Instantiation
Instantiation
- Instantiation is the process of allocating a block of memory to a new object instance. Instantiation is the direct process of declaring a new object by ClassName, giving it an identifier Name, and in some cases passing initialization arguments to the new object. For Example:
Examples
Array Based
1 int[] intArray = new int[5] // Allocates new int array a block of memory
- The above code extract shows the process of declaring a new integer Array, and assigning it to hold 5 items of type integer (using the assignment operator '='). In the background of the program a block of memory large enough to hold 5 integers is allocated to this array and referenced to via the name 'intArray'. The important point here is that the Object instance isnt actually instantiated until it is assigned to, hence if someone tried to do the following:
1 int[] intArray; // Declares a variable called intArray of type integer array
2 Console.WriteLine(intArray[0]);
- An error will be produced due to intArray not referencing anything in memory. Subsequently if 'intArray' is assigned to a new object instance after declaration, the program would work fine.
1 int[] intArray; // Declares a variable called intArray of type integer array
2 intArray = new int[5]; // Assigns variable intArray to a new object instance of type Array with Length 5
3 Console.WriteLine(intArray[0]); // Will output Null
- However unless values in the array are implicitly set after or during initialization, every value in the array will be set to equal Null. This may be confusing as Null does not mean the object takes up no memory it instead means the object has not been allocated to the block of memory created for it during initialization of the array.
Class Based
For a More class Based Example Look Below:
1 // Class Car Inherits from base Object
2 // (Reference To Object Class Unnecessary)
3 class Car(Object) {
4 // No Parameter Constructor
5 public Car() {
6 this.licenseNumber = "L-NUM";
7 this.age = 0;
8 }
9
10 // Parameter Constructor
11 public Car(String licenseNumber, int Age) {
12 this.licenseNumber = licenseNumber;
13 this.age = Age
14 }
15
16 public void Output() {
17 Console.WriteLine("Car With Plate {0} Is {1} Years Old", this.licenseNumber, this.age)
18 }
19
20 public String licenseNumber;
21 public int age;
22 }
23
24 Car myCar1 = new Car() // Initializes new Car Instance Without Passing Any Arguments To Constructor
25 Car myCar2 = new Car("MyLicenseNumber", 4) // Initializes new Car Instance by Passing arguments To Constructor
26
27 myCar1.Output(); // Outputs "Car With Plate L-Num Is 0 Years Old"
28 myCar2.Output(); // Outputs "Car With Plate MyLicenseNumber Is 5 Years Old"
The Example Above Demonstrates the process of object initialization, via separate functionally overloaded constructors. Both Forms of Initializing an Instance Of Class Car, are Viable and result in the same block of memory Being Allocated To Both Instances; the only difference between the two instances are the values of the instance members with one leaving instance members as their default values (as defined in the default parameter less base constructor) and the other setting the values of the instance members during initialization instead of afterwards using 'dot' notation. Lastly The following example illustrates the irrelevance of static instance members to object initialization.