Static and Instance Class Members, Static Vs Instance
Static and Instance Class Members, Static Vs Instance
As well as having members such as properties, methods, and fields that are specific to
object instances, it is also possible to have static (also known as shared, particularly to our
Visual Basic brethren) members, which may be methods, properties, or fields.
Static members are shared between instances of a class, so they can be thought of as global
for objects of a given class.
Static properties and fields enable you to access data that is independent of any object
instances, and static methods enable you to execute commands related to the class type but
not specific to object instances.
When using static members, in fact, you dont even need to instantiate an object.
Classes can expose members as static or instance
Static members work with all instances of the class
Instance members work only with a specific instance
Designer of the class determines which members require a class instance (most do) and
which dont
Unless you specify otherwise
o Members of classes are always instance members
o Each member of the class gets its own copy of the member
Can create members that are shared across all instances
The static keyword indicates that the member associated with all instances, not just one
Property and fields: Just one copy of the data in memory
Methods: Dont create an instance of the class in order to call the method The
class itself provides the static method
Static and instance members live in parallel, but separate universes
o Static members exist only once un memory, apply to all instances of the class
o Main can only interact with static members unless you create an instance of the class