Instantiation

From TRCCompSci - AQA Computer Science
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

For a 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).