Home > other >  Why C# public static variable doesn't need instantiation?
Why C# public static variable doesn't need instantiation?

Time:10-02

I am a bare-metal C, micro-controller guy who is now delving into OOP domain, starting with C# and C .

  1. I understood that, for a member of one class to be accessible by another it has to be of the storage class static and access specifier should be public.
  2. Also, I am able to access public static variable of class2 (initialized with lets say 100), from class1 without creating an object/instance of class 2.
  3. But to access a non-static member public member of the same class one has to instantiate.

Now Questions:

  1. Why we can access a public static variable even when a class is not instantiated ?
  2. Do static variables share the same memory space (in other words are they not different) for two different objects/instances of the same class ?

CodePudding user response:

For your specific questions:


  1. Why we can access a public static variable even when a class is not instantiated?

I would say you instantiate objects rather than classes but it's not relevant to the outcome.

Static members belong to the class, not to any specific object of that class. Hence they can exist whether or not you instantiate any objects of that class.

If they're private, you'll still need to instantiate an object since only objects of that class can access private data. But that's a question of accessibility rather than existence.

Public statics can be accessed from outside the class so no instantiation is required to get at them.


2/ Do static variables share the same memory space (in other words are they not different) for two different objects/instances of the same class?

You could implement statics this way but it would be rather inefficient.

Given a static member belongs to the class, changing it from anywhere you're allowed to change it, must change it for everywhere you can see it.

Hence the most efficient way is to have simply have one of them.

CodePudding user response:

boy I am really weak on C but let me describe using "C concepts" based upon my understanding from Objective-C and OOP in general.

Q1. A: because a public static variable is allocated once at launch-time in the program's single global variable memory space, not in any particular object's (heap-allocated) memory space. An instance variable for an object has its memory located in the space allocated from the heap when that object is created (instantiated). (Objects are more-or-less like malloc()'d structs; instance variables are fields of that struct). So you can read and write public static variables anywhere, anytime, independent of the lifetime of any objects (if any).

Q2. A: static variables are just like global variables in C. each gets its own memory location and allocated an amount of memory corresponding to its size. So of course two variables cannot share the same memory location.

note if the public static variables are themselves OBJECTS of a particular Class, it could be more complicated and def language-specific; I believe C static objects are just like global structs in C. But not necessarily; if the object contains other Objects in its instance variables (i.e. "fields"), and those other objects do tricky things in their constructors, then all bets are off...

  • Related