TOC

This article is currently in the process of being translated into Greek (~26% done).

Classes:

Static members

Όπως είδαμε σε προηγούμενο κεφάλαιο, ο συνήθης τρόπος για να χρησιμοποιήσουμε μια κλάση είναι να δημιουργήσουμε ένα νέο στιγμιότυπο από την κλάση και στη συνέχεια να χρησιμοποιήσουμε το αντικείμενο που προκύπτει. Στις περισσότερες περιπτώσεις, αυτή είναι ουσιαστικά η χρήσημότητα της κλάσης. Η δυνατότητα δημιουργίας πολλαπλών στιγμιοτύπων της ίδιας τάξης και στη συνέχεια η χρήση τους με διαφορετικό τρόπους. Ωστόσο, σε ορισμένες περιπτώσεις, μπορεί να θέλουμε να έχουμε μια κλάση την οποία να μπορούμε να χρησιμοποιήσουμε χωρίς να την αρχικοποιήσουμε ή μπορεί να θέλουμε να χρησιμοποιήσουμε κάποιο μέλος της τάξης χωρίς να δημιουργήσουμε ένα αντικείμενο για αυτήν. Για παράδειγμα, μπορεί να έχουμε μια κλάση με μια μεταβλητή που παραμένει πάντα η ίδια ανεξάρτητα από το πού και πώς χρησιμοποιείται. Αυτό ονομάζεται στατικό μέλος, επειδή παραμένει το ίδιο.

A class can be static, and it can have static members, both functions and fields. A static class can't be instantiated, so in other words, it will work more as a grouping of related members than an actual class. You may choose to create a non-static class instead, but let it have certain static members. A non-static class can still be instantiated and used like a regular class, but you can't use a static member on an object of the class. A static class may only contain static members.

First, here is an example of a static class:

public static class Rectangle
{
    public static int CalculateArea(int width, int height)
    {
        return width * height;
    }
}

As you can see, we use the static keyword to mark the class as static, and then we use it again to mark the method, CalculateArea, as static as well. If we didn't do that, the compiler would complain, since we can't have a non-static member of a static class.

To use this method, we call it directly on the class, like this:

Console.WriteLine("The area is: " + Rectangle.CalculateArea(5, 4));

We could add other helpful methods to the Rectangle class, but perhaps you are wondering why we are passing on width and height to the actual method, instead of storing it inside the class and then pulling them from there when needed? Because it's static! We could store them, but only one set of dimensions, because there is only one version of a static class. This is very important to understand.

Instead, we can make the class non-static, and then have the CalculateArea as a utility function on this class:

public class Rectangle
{
    private int width, height;

    public Rectangle(int width, int height)
    {
        this.width = width;
        this.height = height;
    }

    public void OutputArea()
    {
        Console.WriteLine("Area output: " + Rectangle.CalculateArea(this.width, this.height));
    }

    public static int CalculateArea(int width, int height)
    {
        return width * height;
    }
}

As you can see, we have made the class non-static. We have also added a constructor, which takes a width and a height and assigns it to the instance. Then we have added an OutputArea method, which uses the static method to calculate the area. This is a fine example of mixing static members with non-static members, in a non-static class.

A common usage of static classes, although frowned upon by some people, are utility/helper classes, where you collect a bunch of useful methods, which might not belong together, but don't really seem to fit elsewhere either.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!