4

As a developer I am fan of static methods.

Recently I come across difference between OOP languages about this static methods.

All OOP language use static method to access any method without creating instance of than class. So we can call this kind of method with just class name.

But some language allow to access static method from instance variable also like Java and some don't like Objective - C.

So what is the reason of this kind of difference between two OOP language? Which is correct OOP behaviour? I am just curious to know this.

8

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 a FooClass which eventually is an instance (!= subclass) of NSObject. 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 the instance variable:

class Super {
    public static void staticMethod() { return; }
}
class Instance extends Super {
}

Instance instance = new Instance();
instance.staticMethod();  // doesn't work
((Super) instance).staticMethod(); // works with cast

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”.

  • [[instance class] staticMethod] does not resolve the method based on the static type of instance, but based on the dynamic class of the object pointed to by instance, so it is not like calling a static method on an instance in Java – user102008 Oct 6 '14 at 20:21
  • 5
    Java fun fact: ((MyClass) null).staticMethod() works and does not cause an NPE. It is equivalent to MyClass.staticMethod(). The object reference is never resolved when using it to call a static method, only its variable type is used. – user22815 Oct 28 '14 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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