Understanding static Methods and Data
In the previous exercise you used the Sqrt method of the Math class. If you think about it,
the way in which you called the method was slightly odd. You invoked the method on the
class itself, not an object of type Math. So, what's happening and how does this work?
You will often find that not all methods naturally belong to an instance of a class; they
are utility methods inasmuch as they provide a useful utility that is independent of any
specific class instance. The Sqrt method is just such an example. If Sqrt were an instance
method of Math, you'd have to create a Math object to call Sqrt on:
Math m = new Math();
double d = m.Sqrt(42.24);
This would be cumbersome. The Math object would play no part in the calculation of the
square root. All the input data that Sqrt needs is provided in the parameter list, and the
single result is returned to the caller by using the method's return value. Classes are not
really needed here, so forcing Sqrt into an instance straitjacket is just not a good idea. The
Math class also contains many other mathematical utility methods such as Sin, Cos, Tan,
and Log. Incidentally, the Math class also contains a utility field called PI that we could
have used in the Area method of the Circle class:
public double Area()
{
return Math.PI * radius * radius;
}
In C#, all methods must be declared inside a class. However, if you declare a method or a
field as static, you can call the method or access the field by using the name of the class.
No instance is required. This is how the Sqrt method of the real Math class is declared:
class Math
{
public static double Sqrt(double d) { ... }
...
}
increments the same data every time a new instance is created. You access the
NumCircles field specifying the Circle class rather than an instance. For example:
Console.WriteLine("Number of Circle objects: {0}", Circle.NumCircles);
TIP
Static methods are also called class methods. However, static fields tend not to be called
class fields; they're just called static fields (or sometimes static variables).
Creating a static Field with the const Keyword
You can also declare that a field is static but that its value can never change by prefixing
the field with the const keyword. Const is short for “constant.” A const field does not use
the static keyword in its declaration, but is nevertheless static. However, for reasons that
are beyond the scope of this book to describe, you can declare a field as const only when
the field is an enum, a primitive type, or a string. For example, here's how the real Math
class declares PI as a const field (it declares PI to many more decimal places than shown
here):
class Math
{
...
public const double PI = 3.14159265358979;
}
Static Classes
Another feature of the C# language is the ability to declare a class as static. A static class
can contain only static members (all objects that you create using the class will share a
single copy of these members). The purpose of a static class is purely to act as a holder of
utility methods and fields. A static class cannot contain any instance data or methods, and
it does not make sense to try and create an object from a static class by using the new
operator. In fact, you can't actually create an instance of an object using a static class by
using new even if you wanted to (the compiler will report an error if you try). If you need
to perform any initialization, a static class can have a default constructor, as long as it is
also declared as static. Any other types of constructor are illegal and will be reported as
such by the compiler.
Each time an object is created, its constructor will be called. As long as you
increment the objectCount in each constructor (including the default constructor),
objectCount will hold the number of objects created so far. This strategy works
only because objectCount is a shared static field. If objectCount was an instance
field, each object would have its own personal objectCount field which would be
set to 1. The Point class should now look like this:
class Point
{
public Point()
{
this.x = -1;
this.y = -1;
objectCount++;
}
public Point(int x, int y)
{
this.x = x;
this.y = y;
objectCount++;
}
...
private static int objectCount = 0;
}
Notice that you cannot prefix static fields and methods with the this keyword as
they do not belong to the current instance of the class (they do not actually belong
to any instance).
The question now is this: How can users of the Point class find out how many
Point objects have been created? At the moment, the objectCount field is private