34,333
edits
Changes
no edit summary
In [[An Overview of Objective-C Object Oriented Programming]] we looked in detail about at creating ''instance methods'' and mentioned in passing the existence of another type of method known as ''class methods''. In this chapter we will look at the subject of class methods in more detail and also work through an example of adding some class methods to an Objective-C class.
== Instance and Class Methods ==
Before we delve deeper into the subject of class methods it is first important to explain the how these differ from instance methods. Instance methods are essentially code routines that perform tasks solely on instances of a class. In our BankAccount example we created instance methods to get and set the bank account number and bank balance instance variables and to display the current values of those variables. Since each of instance of the BankAccount class will have its own instance variables containing corresponding instance specific values these methods clearly only operate at the instance level of the class.
Class methods, on the other hand, work at the class level and are common to all instances of a class. In [[An Overview of Objective-C Object Oriented Programming]] we talked about how the BankAccount class was derived from the ''NSObject'' class and as such inherited a number of class methods from this parent. In particular the BankAccount class inherited the ''alloc'' and ''init'' methods used to allocate memory for and initialize instances of the class. Since this these methods are specific to the class overall, as opposed to working on different instance data encapsulated in each class instance, they are considered to be class methods.
== Creating a New Class Method ==
In order to demonstrate the concept of class methods we are going to work on a simplified version of our BankAccount Class class and add write a class method that will count the number of instances of the BankAccount class that have been initiated. To do this we are going to write a new ''alloc'' method for class called ''newAlloc''. The ingredients we need to achieve this are as follows:
* A static variable accessible to all class instances to store the instance count
</pre>
We now have our two class methods declared. Note that since the ''newAlloc'' method is to return a pointer to a block of memory where we will be initializing an instance of a BankAccount class we must declare the return type accordingly. As indicated, the ''totalopentotalOpen'' method will return an integer. Neither method accepts any arguments.
The next step is to work on the implementation of these methods.
== The @implementation Section ==
The first item needed in the implementation section of our class is a variable to contain the current count. Since we want this variable to be visible only within the scope of this file we will declare the variable as ''static'' (the topic of variable scope is covered in detail in [[Objective-C Variable Scope and Storage Class]]). We then need to implement our two new class methods. Lets Let's start by writing the code in our BankAccount.m file and then analyze what we are doing:
<pre>