Understanding static Methods and Data


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) { ... }      ...  }

Bear in mind that a static method is not called on an object. When you define a static method, it does not have access to any instance fields defined for the class; it can only use fields that are marked as static. Furthermore, it can only directly invoke other methods in the class that are marked as static; non-static (instance) methods require creating an object to call them on first.

Creating a Shared Field

As mentioned in the previous section, you can also use the static keyword when defining a field. This allows you to create a single field that is shared between all objects created from a single class, (non-static fields are local to each instance of an object). In the following example, the static field NumCircles in the Circle class is incremented by the Circle constructor every time a new Circle object is created:

class Circle  {

    public Circle() // default constructor      {          radius = 0.0;          NumCircles++;      }        public Circle(double initialRadius) // overloaded constructor       {           radius = initialRadius;          NumCircles++;       }        ...      private double radius;      public static int NumCircles = 0;  }

All Circle objects share the same NumCircles field, and so the statement NumCircles++; 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.

If you were defining you own version of the Math class, containing only static members, it could look like this:

public static class Math  {      public static double Sin(double x) {…}      public static double Cos(double x) {…}      public static double Sqrt(double x) {…}      ...  }

Note, however, that the real Math class is not defined this way as it actually does have some instance methods.

In the final exercise in this chapter, you will add a private static field to the Point class and initialize the field to 0. You will increment this count in both constructors. Finally, you will write a public static method to return the value of this private static field. This field will enable you to find out how many Point objects have been created.

Write static members and call static methods

  1. Using Visual Studio 2005, display the Point class in the Code and Text Editor window.

  2. Add a private static field called objectCount of type int to the end of the Point class. Initialize it to 0 as you declare it.

    The Point class should now look like this:

    class Point  {      ...;      private static int objectCount = 0;  }

    NOTE
    You can write the keywords private and static in any order. The preferred order is private first, static second.

  3. Add a statement to both Point constructors to increment the objectCount field.

    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 and not available outside the class. A poor solution would be to make the objectCount field publicly accessible. This strategy would break the encapsulation of the class; you would then have no guarantee that its value was correct because anything could change the value in the field. A much better idea is to provide a public static method that returns the value of the objectCount field. This is what you will do now.

  4. Add a public static method to the Point class called ObjectCount that returns an int but does not take any parameters. In this method, return the value of the objectCount field.

    The Point class should now look like this:

    class Point  {      public static int ObjectCount()      {          return objectCount;      }      ...  }

  5. Display the Program class in the Code and Text Editor window, and locate the Entrance method.

  6. Add a statement to the Entrance method to write the value returned from the ObjectCount method of the Point class to the screen.

    The Entrance method should look like this:

    static void Entrance()  {      Point origin = new Point();      Point bottomRight = new Point(600, 800);      double distance = origin.distanceTo(bottomRight);      Console.WriteLine("Distance is :{0}", distance);      Console.WriteLine("No of Point objects :{0}", Point.ObjectCount());  }

    The ObjectCount method is called by using Point, the name of the class, and not the name of a Point variable (such as origin or bottomRight). Because two Point objects have been created by the time ObjectCount is called, the method should return a value of 2.

  7. On the Debug menu, click Start Without Debugging.

  8. Confirm that the value of 2 is written to the console window (after the message displaying the value of the distance variable). Press Enter to finish the program and return to Visual Studio 2005.

Congratulations. You have successfully created a class, and used constructors to initialize the fields in a class. You have created instance and static methods, and called both of these types of methods. You have also implemented instance and static fields. You have seen how to make fields and methods accessible by using the public keyword, and how to hide them using the private keyword.

  • If you want to continue to the next chapter

    Keep Visual Studio 2005 running, and turn to Chapter 8.

  • If you want to exit Visual Studio 2005 now

    On the File menu, click Exit. If you see a Save dialog box, click Yes.




Microsoft Visual C# 2005 Step by Step
Microsoft® Visual C#® 2005 Step by Step (Step By Step (Microsoft))
ISBN: B002CKYPPM
EAN: N/A
Year: 2005
Pages: 183
Authors: John Sharp

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net