Hi, What's the difference between a class method and an instance method? Are instance methods the accessors (getters & setters) while class methods are pretty much everything else? Thanks,
An instance method applies to an instance of the class (i.e. an object) whereas a class method applies to the class itself.
In C# a class method is marked static. Methods and properties not marked static are instance methods.
class Foo {
public static void ClassMethod() { ... }
public void InstanceMethod() { ... }
}
Instances methods operate on instances of classes (ie, "objects"). Class methods are associated with classes (most languages use the keyword static
for these guys).
The answer to your question is not specific to objective-c, however in different languages, Class methods may be called static methods.
The difference between class methods and static methods are
Class methods
- Operate on Class variables (they can not access instance variables)
- Do not require an object to be instantiated to be applied
- Sometimes can be a code smell (some people who are new to OOP use as a crutch to do Structured Programming in an OO enviroment)
Instance methods - Operate on instances variables and class variables - Must have an instanciated object to operate on
Like most of the other answers have said, instance methods use an instance of a class, whereas a class method can be used with just the class name. In Objective-C they are defined thusly:
@interface MyClass : NSObject
+ (void)aClassMethod;
- (void)anInstanceMethod;
@end
They could then be used like so:
[MyClass aClassMethod];
MyClass *object = [[MyClass alloc] init];
[object anInstanceMethod];
Some real world examples of class methods are the convenience methods on many Foundation classes like NSString
's +stringWithFormat:
or NSArray
's +arrayWithArray:
. An instance method would be NSArray
's -count
method.
Like the other answers have said, instance methods operate on an object and has access to its instance variables, while a class method operates on a class as a whole and has no access to a particular instance's variables (unless you pass the instance in as a parameter).
A good example of an class method is a counter-type method, which returns the total number of instances of a class. Class methods start with a +, while instance ones start with an -. For example:
static int numberOfPeople = 0;
@interface MNPerson : NSObject {
int age; //instance variable
}
+ (int)population; //class method. Returns how many people have been made.
- (id)init; //instance. Constructs object, increments numberOfPeople by one.
- (int)age; //instance. returns the person age
@end
@implementation MNPerson
- (id)init{
if (self = [super init]){
numberOfPeople++;
age = 0;
}
return self;
}
+ (int)population{
return numberOfPeople;
}
- (int)age{
return age;
}
@end
main.m:
MNPerson *micmoo = [[MNPerson alloc] init];
MNPerson *jon = [[MNPerson alloc] init];
NSLog(@"Age: %d",[micmoo age]);
NSLog(@"%Number Of people: %d",[MNPerson population]);
Output: Age: 0 Number Of people: 2
Another example is if you have a method that you want the user to be able to call, sometimes its good to make that a class method. For example, if you have a class called MathFunctions, you can do this:
+ (int)square:(int)num{
return num * num;
}
So then the user would call: [MathFunctions square:34], without ever having to instantiate the class!
You can also use class functions for returning autoreleased objects, like NSArray's
+ (NSArray *)arrayWithObject:(id)object
That takes an object, puts it in an array, and returns an autoreleased version of the array that doesn't have to be memory managed, great for temperorary arrays and what not.
I hope you now understand when and/or why you should use class methods!!
Now that we all know WHAT they are... now the big question:
So when does Apple (or me, or anyone) choose to use a CLASS METHOD instead of an INSTANCE METHOD?
NSString *myString = @"this is only a test";
s = [MyClass removeVowels:myString];
or
s = [myString removeVowels];
Which is somehow "better"?
(And without writing the actual 'removeVowels" code... what would the 2 different methods look like?)