Home > Articles > Home & Office Computing > Mac OS X

  • Print
  • + Share This
From the author of Static Behavior

Static Behavior

In Java, classes can have static fields and methods. A static field is effectively equivalent to a file static in C; it's a variable that has the lifespan of the program but has its visibility restricted to the compilation unit in which it is declared (or the class, in Java). A static method is effectively a namespaced function.

Objective-C has functions inherited from C. There is no direct equivalent of a static method, however. Classes in Objective-C are real objects. You can take a pointer to a class just as you would with an object and send it messages. This lets you do some quite interesting things that are difficult in Java. One example is having a dictionary mapping from strings to classes and creating a new class depending on which string you parse. This is incredibly useful, for example, when parsing XML and wanting a different parser class for each element that you might parse.

  • + Share This
  • 🔖 Save To Your Account