Static Fields
Besides instance fields, classes can have what are called static fields.
A static field is shared by all the instances of the class, and all the instances access the same memory location. Hence, if the value of the memory location is changed by one instance, the change is visible to all the instances. Use the
For real world example let's take a look at vehicle. vehicle has number of wheel, name, gas consumption,etc. Gas price will be same for all vehicle,
no mater it's car, bus, truck the gas price will be the same. That's what we call static field.
Following code is simple vehicle encapsulation:
The code will produce following display:
This is what in memory look like:
A static field is shared by all the instances of the class, and all the instances access the same memory location. Hence, if the value of the memory location is changed by one instance, the change is visible to all the instances. Use the
static
modifier to declare a field static, as follows:
[Access Modifier:Optional] static [Type][Identifier];
Following code is simple vehicle encapsulation:
class vehicle { string name; ushort wheelNumber; float gasCosumption; static decimal gasPrice; public vehicle(string veName, ushort veWheelNumber, float veGasConsumption) { name = veName; wheelNumber = veWheelNumber; gasCosumption = veGasConsumption; } public void setGasPrice(decimal currentPrice) { gasPrice = currentPrice; } public void displayGasPrice() { Console.WriteLine("Current Price is for " + name + ": " + gasPrice + " IDR"); } } class Program { static void Main(string[] args) { vehicle car = new vehicle("Car", 4, 0.345F); vehicle bus = new vehicle("Bus", 6, 0.786F); car.setGasPrice(9500); car.displayGasPrice(); bus.displayGasPrice(); } }
The code will produce following display:
Current Price is for Car: 9500 IDR
Current Price is for Bus: 9500 IDR
Current Price is for Bus: 9500 IDR
This is what in memory look like:
Static members exist even if there are no instances of the class. If a static field has an initializer, the field is
initialized before the use of any of the class’s static fields, but not necessarily at the beginning of program execution.
Static Methods
Just the same with fields, methods also can be modified as static. With restrictions as follow:
- Static function members, like static fields, are independent of any class instance. Even if there are no instances of a class, you can still call a static method.
- Static function members cannot access instance members. They can, however, access other static members.
setGasPrice
methods to static and take a look to the effect.
.... public static void setGasPrice(decimal currentPrice) { gasPrice = currentPrice; } .... static void Main(string[] args) { vehicle car = new vehicle("Car", 4, 0.345F); vehicle bus = new vehicle("Bus", 6, 0.786F); vehicle.setGasPrice(9500); car.displayGasPrice(); bus.displayGasPrice(); car.setGasPrice(9500); //compile error can not access static method from instance }
Other Static Class Member Types
The types of class members that can be declared static are shown on following table.
Data Members (Store Data) | Function Members (Execute Code) |
---|---|
Fields | Types |
Constants | Methods |
Properties | Constructors |
Operators | |
Indexers | |
Events |
0 comments:
Post a Comment