display | more...

In object oriented programming a method which performs some operation in a class, as opposed to performing it on a particular instance (like instance methods do). They are also called static methods in C++ and Java.

One special type of class method is the constructor, because it's called upon the class, and can't be inherited. What makes a constructor a special kind of constructor in Java and C++ is that its scope is "adjusted" to that of the instance being created. But don' t take my word for this, few people believe me when I tell them this.. =).

A method with class.

Bank robbery and swindling are both methods to separate fools from their money, but bank robbery involves violence, potentially death and a long prison sentence if you get caught; swindling involves poise, timing, manners and intelligence and if you get caught they're less inclined to lock you away with a 180 pound gorilla called Bubba. Thus swindling is a class method and bank robbery isn't.

see also: classy, Miss Manners and snobbery

A constructor is a very special type of class method, and in some languages I wouldn't call it one at all: In C++, for example, when you create an instance of a class, the constructor for that class is called for that instance. It can (and should) access all the data members of that instance, be they static or be they not: That's how they get initialized.. In JavaScript, if I recall correctly, this is handled very differently. The only OOP language I know at all well is C++ (unless you count ObjectPascal, which I'd rather not admit to knowing), so I'll confine myself to that. From here on in I'll refer to methods by the C++ese term "member functions", lest we forget that I wouldn't know Smalltalk if it bit me on the ass.

This is the key point about static member functions: They can't access anything particular to the instance through which they're called, and in fact you can call them without specifying any instance at all. In C++, the static member function foo() of class bar could be called like so: bar::foo(), and it could also be called through instance baz of that class: baz.foo().

What use is a static member function? Good question. You might have one or more static data members 1, in which case you might want to protect them from subclasses by making them private and allowing access only through a safe and sane procedural interface. There are a few reasons why a class might want to have data common to all instances of the class. You might also have a function which is specific to your class and which ought to be closely associated with it, but which doesn't need to access the data members of any particular instance.

Another use is this, and it's the only one I've found consistently useful: If you're making class wrappers for a "message" based windowing API (Win32, for example), how do you route messages to the correct class instance? The gory details are a matter for another writeup: Window Class Library.

1 F'rinstance, I once wrote some undo/redo classes which had static data members that tracked the total amount of memory allocated by all living instances of classes anywhere in the hierarchy. When they allocated memory , they'd add to the total, and when they freed memory, they'd subtract from the total. This allowed me to set a limit on the total memory used by the whole mess, and to enforce it accurately. The static member functions handling the count would nibble away at the bottoms of the stacks when I went over the limit. A better way might have been to define a class that would handle the count and notify the stacks when they needed to prune themselves. I could then have had several independent and unrelated sets of undo/redo information; just pass around pointers to memory tracking objects as needed. But I digress.

Log in or register to write something here or to contact authors.