Start New Discussion within our Software Development Community

I was learning about the static class and static method.Then i came across the following line:
Only one copy of static member exists regardless of any no of instance of a class.
what does it actually mean?

It is in the msdn document achfcssswwws follows:

A non-static class can contain static methods, fields, properties, or events. The static member is callable on a class even when no instance of the class has been created. The static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created. Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.

Say you have a static variable, counter, in your class and a method that incremented and displayed counter. You then create two instances of the class, X and Y.
Calling x.increment() will output 1. The static variable becomes 1.
Calling y.increment will output 2 because it accesses the same static variable counter e.g. the variable accessed and incremented by X is the same variable accessed and incremented by Y.
Hence, you can never have 2 versions of counter. It will be shared across all instances of the class regardless of how many you create.
Does that help?

This question has already been answered. Start a new discussion instead.