Instantiation

From TRCCompSci - AQA Computer Science
Revision as of 13:14, 15 December 2016 by Admin (talk | contribs)
Jump to: navigation, search

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, and the ability to take advantage of static members to add functionality to classes.

 1 // Class Animal Inherits from base Object 
 2 // (Reference To Object Class Unnecessary)
 3 Class Animal(Object) {
 4     public Animal(String animalName, int numOfLegs) {
 5         this.animalName = animalName;
 6         this.numOfLegs = numOfLegs;
 7         Animal._animalCount += 1;
 8         // Increments Count Of Animals
 9         // Every Time Constructor Is Called
10     }
11 
12     public static void OutputACount() {
13         Console.WriteLine("Animal Count : {0}", Animal.animalCount);
14     }
15 
16     public String animalName;
17     public int numOfLegs;
18     
19     private static int _animalCount;
20     public static int animalCount { get return _animalCount; };
21 }
22 
23 Animal an1 = new Animal("Chuhaha", 4);
24 Animal an2 = new Animal("Monkey", 2);
25 
26 Animal.OutputACount(); // Outputs 2
27 Animal.OutputACount(); // Outputs 2
28 
29 Animal an3 = new Animal("Neanderthal", 2);
30 Animal.OutputACount(); // Outputs 3

The Above Example Shows That Regardless Of How An Instance Is Initialized, Static members are unaffected unless expressly set to; Hence a static instance member unlike a constant value, can be changed during every initialization of a given class (into an object), regardless of a given users intent towards that value (such as forcefully setting animalCount to new values, which in this case will just produce an error).