Posted 16 years ago on 10/31/2008 and updated 1/20/2009
Take Away:
A static member is a member you can have access to without instantiating the class into an object. For example, you can read and write static properties and call static methods without ever creating the class. Static members are also called class members (class methods, class properties, etc.) since they belong to the class and not to a specific object. A static class is a class that contains only static members. In the UML, these classes are described as utility classes.
A static member is a member you can have access to without instantiating the class into an object. For example, you can read and write static properties and call static methods without ever creating the class. Static members are also called class members (class methods, class properties, etc.) since they belong to the class and not to a specific object. A static class is a class that contains only static members. In the UML, these classes are described as utility classes.
Static Methods
A.k.a. Static Member
Depending on the lanaguage, you can make any class member static (methods, fields, properties, and even events). Using static methods is similiar to using global functions and data in languages that support global functions and data (like Object Pascal and C++). In languages that don't support global functions and data, you may end up using a lot of static methods and static data.
Calling Static Members
When calling a static method, you do so through the class itself and not through the object instantiated. Typically you use ClassName.StaticMethodName. If you use the ObjectInstanceName.StaticMethodName, you will either get an error or the language will allow the usage but the compiler will use the class even though you specified the object. For example, C# and VB.Net require the class name, but with Java you can use either the class or object name. However, with languages that allow you to use the object instance, it is strongly recommended that you don't because of the potential confusion.
Static Fields
In general, any method (static or not) can access a static field. However, a static method cannot access it's own non-static member fields.
Why use static methods
Faster Code - Many compilers will optimize static methods because no object instance is involved (passed).
Less Source Code - Since you don't have to create an object instance, you will have less lines of code.
Documentation - Since static methods are not associated with an object instance, it is clear when looking at code that uses the syntax ClassName.Method that it is a static method associated with the class and not associated with a particular object instance.
Class vs. Object Data Rule of Thumb
The data used by static methods is the same for all objects because it is stored up at the class level. Object data is unique for each object instance (and represents the current state of the object). One interesting rule of thumb that is somewhat popular is, "Always use static methods UNLESS you need the data to be different for each object instance." Like all rules of thumb, it's interesting because it stresses a particular point or feature. Don't over use this rule of thumb but it is interesting and does bring up several design discussions such as, "If a method doesn't manipulate an object instance's attributes, does the method really belong to this class?"
Static Classes
Use a static class to organize your static methods not associated with particular objects. A static class can make your implementation simpler and faster because you do not have to create an object in order to call its methods. Consider grouping your static methods in static classes for organization. In non-OOP languages it is common to group functions in a way that organizes the routines such as strings, math, etc.
A static member is a member you can have access to without instantiating the class into an object. For example, you can read and write static properties and call static methods without ever creating the class.
i think should be: ... without ever creating the object.