"Static" means two things:
1. The method can be called on the class directly, rather than requiring an
instance of the class. For example:
x = myClass.someStaticMethod(100); // this is fine.
myClass M = new myClass;
x = M.someInstanceMethod(100); // this is fine.
x = myClass.someInstanceMethod(100); // this is not allowed
Although I believe that you can also call static methods via an instance of
the class.
2. All objects of a given class share the same copy of the static method.
Because of this, a static method cannot make use of "this" like an instance
method can. A static method can access any static fields of the class, but
not anything that would have to have been initialized in a constructor.
Generally, static methods are good if you want people to have easy access to
some version of your class that might otherwise take several steps to
create, or that would require knowledge of the inner details of the class.
For example, let's say you had a Matrix class for representing 2-d matrices.
You might want to make it easy for people to get an Identity matrix of a
given size. One way would be to make them say:
Matrix M = new Matrix(5); // I want a 5x5 matrix
for(int i = 0; i < 5; i++)
for(int j=0; j < 5; j++)
if(i==j)
M[i,j] = 1; // we'll assume that Matrix has a 2-d indexer
property.
else
M[i,j] = 0;
Well, that's kind of a pain for them. It would be better, if in your class,
you had a method like this:
public class Matrix {
// code for creating a matrix
public static Matrix Identity(int size) {
Matrix M = new Matrix(size);
// same nested for-loop as above
return M
}
}
That way, someone using your class who wants a 5x5 Identity matrix can
simply say:
Matrix M = Matrix.Identity(5);
Much simpler.