4

I'm learning programming in Objective-C and I can't understand what a static method is. I know what class/instance/private/public methods are. Can someone explain what it is using an example and comparing it to the others I mentioned? And in what situation would someone use it?

From what I read on Wikipedia, it sounds like a class method that can't access anything in self. This sounds to me like its a stand-alone function - that shouldn't be associated with any class in the first place, since it can't access self.

1
  • You are right, it is a stand-alone function that can't access anything in self. There is no technical need to associate it with a class, nevertheless it can make a lot of sense to keep it there (for example, to keep the complete source code of a class file, including static "helper methods", together in a logical unit).
    – Doc Brown
    Mar 25, 2013 at 16:26

3 Answers 3

5

I'm learning programming in Objective-C and I can't understand what a static method is.

Objective-C doesn't have "static methods". It also doesn't have private methods. It does have class methods (marked with a +) and instance methods (marked with a -). Class methods are similar to static methods in C++ in that you can call them without having an instance of the class that defines them, so I expect you're asking about class methods and why they're useful.

In Objective-C, classes themselves are a kind of object, and you can send messages to them just as you send messages to an instance of a class (although the messages that you can send are different). Class methods are used whenever you want the class to do something but don't have an instance of the class. They're often used for getting or creating instances of the class. A perfect example is the +alloc method. +alloc creates an instance of a class by allocating memory for that instance; it's then up to you to initialize the instance with an appropriate initializer:

Foo *f = [[Foo alloc] initWithBar:bar];

Here we're sending the message alloc to the object that is the Foo class itself in order to create an new instance of Foo. Then we're sending the message initWithBar: to the new instance in order to initialize it with the object bar.

Similarly, class methods are often used as "convenience constructors" to create an object in one step rather than the two above:

NSString *s = [NSString stringWithFormat:@"%f", someFloat];

Here we just send a message to the class NSString (remember, classes are themselves objects) and get back a properly initialized string.

Without class methods, there would be no way to create instances of classes.

Sometimes a class manages access to a shared instance of itself. In those cases, the class will often provides a method to retrieve that shared object:

NSFileManager *fileManager = [NSFileManager defaultManager];

Most of the time you don't need to create your own NSFileManager instance -- you can just use the shared object. Having the NSFileManager class manage that object makes sense -- it's easy to figure out how to get the shared instance, and the way the shared file manager is handled may change as the implementation of NSFileManager changes.

From what I read on Wikipedia, it sounds like a class method that can't access anything in self.

self is valid in a class method, but it refers to the class object and not to any instance of the class. So you're right that you can't access any instance variables from within a class method, but that doesn't mean that the message should be a "stand-alone function" entirely unrelated to the class.

2
  • Thanks for the explanation and examples. Regarding private methods, I thought they do exist. If you don't declare the method in the header (.h) file, then isn't that method private?
    – abc123
    Mar 25, 2013 at 16:28
  • @SKG If you don't declare the method in the header file then it may be harder to find, but nothing other than obscurity and good manners prevents client code from sending that message. An easy way to do call a method not listed in the header file is to use one of the -performSelector:... methods. One way to find methods not listed in the header is to query the Objective-C runtime using class_copyMethodList(). You really shouldn't need to do that, though.
    – Caleb
    Mar 25, 2013 at 16:34
3

Yes, a static class method belongs to a class but not to a specific object of this class. So it can't access any data in an object.

Static methods are used to provide functionality on the class level. These should of course still be related to this class. There are many reasons why you could use this. For example you may have a class that needs many parameters for object creation and want to have a few "default setups", then you would write a static method that returns an object of the class. Or you want methods that work on lists of objects like for example calling their print methods.

-1

Caleb's answer is very thorough, but the bit in bold about not being able to create instances without class methods is not entirely true. Objective-C being a hybrid language and all, you can definitely create instances without class methods.

One easy way is to call [NSAllocateObject( NSClassFromString(@"NSObject" ),0,NULL) init];

Substitute other classes to taste. You can of course obtain the class using other means, and if you want you can just calloc() a piece of memory and add the class pointer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.