(Using Java terminology): Static methods can be associated with static members (members of the class
object). If the methods don't access static members (nor IO resources or anything else that could change state), of if those members are final
primitives or immutable objects (essentially constants), then such static methods could be called functions, but in general, because they potentially could access the class
object's members, they're usually called static methods.
In order to count as a function, a method must be independent of any state; it must not cause any side effects nor be affected by any side effects. When called with specific parameters, it must always return the same result. Otherwise it's not a pure function.
That is, the following area
is a function:
class MyClass {
static final double pi = 3.14;
static double area(double r) {
return pi * r * r;
}
}
while the following getCallCount
is not a function:
class MyClass {
static int callCount = 0;
static int getCallCount {
return ++callCount;
}
}
In general, mutable (non-final
) static members should be used with caution - someone would say that they should not be used at all - because they make a global state to the program, which likely turns out to be a bad design choice in the long run. There are exceptions, but be careful...
You don't even need static members to make static non-functions: System.nanoTime()
is definitely not a pure function (because it returns different value on successive calls), even though it only accesses the computer's clock, not any static members.
Confusingly enough, you could also make non-static pure functions:
class MyClass {
final double pi = 3.14;
double area(double r) {
return pi * r * r;
}
}
Now, although not static
any more, area
is still a pure function: it doesn't touch anything that could change. The fact that you would have to create an instance of MyClass
to access area
doesn't reduce its "functionness". Indeed, one could argue that this kind of pure functions should always be made static.
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.
Best Answer
Static methods have nothing to do with OOP at its core. If Java had no instances of classes, this would be an example of modular programming with classes being modules and static methods being ordinary procedures.
The here relevant difference between Java and Objective-C is that the latter has a Smalltalk-like metaobject protocol: classes themselves are normal objects. A
foo
object may be an instance of aFooClass
which eventually is an instance (!= subclass) ofNSObject
. When we invoke a method on a class, we send this message to the actual object representing that class. In Java, calling a static method does not involve objects at all, it's just a procedure call (and can be fully resolved before run time – no dynamic dispatch is needed).In Java, classes are not reified beyond reflection. So
instance.staticMethod()
kind of makes sense, as it couldn't mean anything else (it just happens to share the syntax for ordinary method calls, but the static method is only looked up according to the static type of theinstance
variable.Objective-C does not have such “static methods”, but it does have:
class methods, which resemble static methods if you squint your eyes. It would be very confusing if a method call on an instance would end up being a method call on a class (another object alltogether), so there is no syntactic shortcut like
[instance staticMethod]
, which doesn't even save that much over[[instance class] staticMethod]
. Such class methods are good for constructors and other factory methods. However, they are not “static” as they are subject to inheritance.plain old C functions. These exist outside of objects and classes, and are not subject to dynamic dispatch – which also makes them interesting from a performance standpoint. These are “static” in the actual sense of that word, but they are not “methods”.