Objects
What is an object?
An object is an instance of a class with attributes and behaviours that are defined in its class.
How do you create an object?
To create an object from the Scooter class you need the following code. st is the name of the object.
Scooter st = new Scooter();
How do you use an object?
You can access public member methods of an object by typing a full stop after the object name.
st.Accelerate();
How do I create multiple objects?
You can create multiple objects of the same class. Each will behave completely independently of the other. The following code creates two Scooter objects named s1
and s2
. The objects have the same attributes and behaviours (as defined in the Scooter class). However the method Accelerate()
called for the s2
object has no effect on the s1
object at all.
Scooter s1 = new Scooter();
Scooter s2 = new Scooter();
s2.Accelerate();
How do I store many objects together?
An array allows you to store multiple items of the same type. You can store simple data types (see here) or objects. An array is a fixed size. Lists can dynamically grow or shrink (see here).
Step 1: Create an array
The following code will create an array which holds 20 objects of the student class:
Student[] myStudent = new Student[20];
Step 2: Create an object
Student s = new Student("Bart", "Simpson");
Step 3: Assign the object to the array
The square brackets are used to give the position in which to store each item. Arrays start at position 0, so the cold below will store the student object s in the first position in the array.
myStudents[0] = s;
Retrieving one object from the array:
You can retrieve an object at a given position. Here is the code to get the first object.
Student s = myStudents[0];
Retrieving all objects from the array:
You can loop through the array and retrieve on object at a time. Here is the easiest way:
foreach (Student s in myStudents)
{
// s is the current object
}
Checking that an object exists
Some methods might retrieve an object for you. In the example below the method PerformMysteryCalc()
will take the two argument variables passed in and perform an unknown calculation. The result will be returned in the Result
object myResult
.
Result myResult = PerformMysteryCalc(3, 7);
// call the Print member method of myResult
myResult.Print();
However, PerformMysteryCalc()
will only work with a positive integer. If a negative integer is passed into the method, the method will return null. This means that the object. This means that it doesn't really exist as an object and can not be referenced. The following code will cause a null pointer exception error.
Result myResult = PerformMysteryCalc(-1, 7);
// call the Print member method of myResult
myResult.Print();
The error is caused because the myResult object returned will be null (a negative value has been passed to the method). Thus we can not access any methods or variables at all. The attempt will cause the error. To complete the code, you must check for null as follows:
Result myResult = PerformMysteryCalc(-1, 7);
// check if the myResult object exists before calling the Print member method.
if (myResult != null)
{
myResult.Print();
}