Difference between revisions of "Instantiation"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Instantiation)
(Instantiation)
Line 11: Line 11:
 
</tabber>
 
</tabber>
  
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:
+
: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:
  
 
<tabber>
 
<tabber>
Line 21: Line 21:
 
</tabber>
 
</tabber>
  
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.
+
: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.
  
 
<tabber>
 
<tabber>
Line 32: Line 32:
 
</tabber>
 
</tabber>
  
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.
+
: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====
 
====Class Based====

Revision as of 12:27, 15 December 2016

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"