Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between "class method" and "static method"?

I've worked with a few different languages such as Java, C#, and Objective-C.

In most languages, methods that don't require an instance of an object are called static methods. However, when it comes to Objective-C, some people get defensive when you call them static methods, and they expect you to call them class methods.

Why are they called class methods instead of static methods? What is the difference between a static method and a class method?

like image 561
aryaxt Avatar asked Nov 11 '11 03:11

aryaxt


People also ask

What is the difference between class method and static method?

Class method can access and modify the class state. Static Method cannot access or modify the class state. The class method takes the class as parameter to know about the state of that class. Static methods do not know about class state.

What is the difference between static method and class method in Java?

The class method can access the class variables. The static method cannot access the class variables and static variables. Therefore, it cannot change the behavior of the class or instance state.

What is the difference between static class and class with static methods?

A static class can only contain static members. A static method ensures that, even if you were to create multiple classB objects, they would only utilize a single, shared SomeMethod function. Technically, there's no difference, except that ClassA's SomeMethod must be static.

What is difference between method and class method?

The main difference between Class and Method is that class is a blueprint or a template to create objects while method is a function that describes the behavior of an object. A programming paradigm is a style that explains the way of organizing the elements of a program.


2 Answers

So my question is why are they called class methods instead of a static method? What is the difference between a static method and a class method?

From Wikipedia: Static methods neither require an instance of the class nor can they implicitly access the data (or this, self, Me, etc.) of such an instance.

This describes exactly what Objective-C's class methods are not.

An Objective-C class method very much requires an instance that is the target of the method invocation. That is, it requires an instance of the metaclass that describes the class object being invoked.

Unlike static methods, Objective-C's class methods can be inherited (which, in combination with having the aforementioned self, is exactly why many classes can share a single, simple, implementation of +alloc on NSObject without needing their own custom implementations) and invoking a class method goes through the exact same objc_msgSend* based dispatch mechanism as any other method call site.

Objective-C's class methods can be overridden across the class hierarchy and they can be swizzled. None of which is supported in languages that typically offer static methods in lieu of class methods.

The bottom line is that static methods and class methods are very different. While that difference is mostly transparent for day to day coding purposes, there are still situations where knowing how class methods work can save you a ton of unnecessary lines of code.

For example, you can't do this with static methods:

@interface AbstractClass:NSObject + factory; @end  @implementation AbstractClass + factory {     return [[[self alloc] init] autorelease]; } @end  @interface Concrete1:AbstractClass @end @implementation Concrete1 @end @interface Concrete2:AbstractClass @end @implementation Concrete2 @end  void foo() {     Concrete1 *c = [Concrete1 factory];     Concrete2 *d = [Concrete2 factory];     ... etc ... }     
like image 157
bbum Avatar answered Sep 20 '22 12:09

bbum


Because it's dynamically bound, not static.

Because it's really a class object's instance method.

Objective-C class method is actually an object's class object's instance method.

It's hard to describe with text. See nice illustration here.

http://www.sealiesoftware.com/blog/archive/2009/04/14/objc_explain_Classes_and_metaclasses.html

like image 40
eonil Avatar answered Sep 19 '22 12:09

eonil