What is a static method compared to instance/class/private/public methods?

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

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

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.

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật