Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I was just reading http://stackoverflow.com/questions/155609/what-is-the-difference-between-a-method-and-a-function and all of a sudden, the thing came to my mind was the static methods.

As static methods are no way associated with an object how can it be called as static methods (almost all developers do)? Is my understanding about the methods/functions is correct?

share|improve this question

3 Answers 3

up vote 9 down vote accepted

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

share|improve this answer

I agree with you. I usually call them procedures, since they are neither methods (associated with a dynamic object), nor functions (side-effect free).

share|improve this answer

As static methods are no way associated with an object how can it be called as static methods (almost all developers do)? Is my understanding about the methods/functions is correct?

That's an abuse of language. In C++ :

  • a member function is a function associated with a type or instances of a type. It can be:
    • static : associated with the type itself, not with instances
    • non-static : associated with an instance of the type. They can be :
    • virtual : can be redefined by a child class of the type
    • non-virtual : cannot be redefined by a child class (but can be "hidden", often trigerring a compilation warning)
  • a method is a virtual member function that cannot be static by definition

The problem is that some post-C++ languages like Java, a all (non-static) member functions are virtual. So people started to say those are methods. As they didn't needed non-virtual member-functions, they only used the word method for any function that is member of a class. Now, in those languages, types are also objects, are one instance, so you can say that their static members are in fact methods of the unique object represeting the type. (I'm not sure that's correct for any dynamic language)

Anyway, from your perspective, static member functions(or method if you prefer) should always be perceived as independant of the objects of the type they are member of, but implicitely associated with the type itself (whatever the implementation of that meaning is - whatever the language).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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