Chapter 8 Object-Based Programming (Disney web site) 313 We now consider
Chapter 8 Object-Based Programming 313 We now consider a video-game example to justify the need for static class-wide data. Suppose that we have a video game involving Martians and other space creatures. Each Martian tends to be brave and willing to attack other space creatures when the Martian is aware that there are at least four other Martians present. If there are fewer than five Martians present, each Martian becomes cowardly. For this reason, each Martian must know the martianCount. We could endow class Martian with martianCount as instance data. However, if we were to do this, then every Martian would have a separate copy of the instance data, and, every time we create a Martian, we would have to update the instance variable martianCount in every Martian. The redundant copies waste space, and updating those copies is time-consuming. Instead, we declare martianCount to be static so that martianCount is class-wide data. Each Martian can see the martianCount as if it were instance data of that Martian, but C# maintains only one copy of the static variable martianCount to save space. This technique also saves time; because there is only one copy, we do not have to increment separate copies of martianCount for each Martian object. Performance Tip 8.2 When a single copy of the data will suffice, use static variables to save storage. Although static variables might seem like global variables (variables that can be referenced anywhere in a program) in other programming languages, static variables need not be globally accessible. static variables have class scope. The publicstatic data members of a class can be accessed through the class name using the dot operator (e.g., Math.PI). The privatestaticmembers can be accessed only through methods or properties of the class. staticmembers are available as soon as the class is loaded into memory at execution time and they exist for the duration of program execution, even when no objects of that class exist. To enable a program to access a private static member when no objects of the class exist, the class must provide a publicstatic method or property. A static method cannot access instance (non-static) members. Unlike instance methods, a static method has no this reference, because static variables and static methods exist independently of any class objects, even when there are no objects of that class. Common Programming Error 8.5 Using the this reference in a static method or static property is a compilation error. Common Programming Error 8.6 A call to an instance method or an attempt to access an instance variable from a static method is a compilation error. Class Employee (Fig. 8.13) demonstrates a publicstatic property that enables a program to obtain the value of a privatestatic variable. The static variable count (line 11) is not initialized explicitly, so it receives the value zero by default. Class variable count maintains a count of the number of objects of class Employee that have been instantiated, including those objects that have already been marked for garbage collection, but have not yet been reclaimed by the garbage collector.
You want to have a cheap webhost for your apache application, then check apache web hosting services.