This concept which I am going to discuss is a very basic concept, yet often a confusing concept for beginners, and you will find many articles about this topic on the internet; but today I am going to discuss some new things.
This articles gives you the best knowledge about this topic. Read it, I believe definitely you will get something new.
Before going on to discuss our topic let’s first understand what is means by static member and instance member.
In C# all the data members which can be initialized at the time of object creation are called Instance member and the members which aren't required objects to initialize the variable are called Static member.
Now let’s starts with Static variable Vs Instance variable.
- By default every variable under a class is Instance variable unless it is declared by using a static modifier or declared under static block.
Example:
- Class Demo
- {
- int x,
-
- static int a;
- static void Display()
- {
- int b = 20;
- Console.WriteLine(a + ” ” +b);
- }
- Static void Main()
- {
- Console.writeLine(a);
- Display();
-
- }
- }
In the above example I have declared variable ‘a’ as static ,so this variable got Initialized by it’s default value when the class loading happen and here b is also a static variable because it's declared under a static block
Key Capsule: Here important thing to remember is that when we are using static modifier to any of the variable that must be initialized only once at the class loading time and the variable if declared under static block then that variable also a static variable.
Static VS Instance Variable
- A static variable of class get initialized immediately once the execution of class starts or we say at the class loading time, whereas an instance variable get initialized only after creating the object of that class.
Key Capsule: It’s important to remember that when we call the Constructor of that class using new keyword at that moment only the instance variable got initialized .The number or time we call the constructor as that many time the variable got Initialized.
- In the life cycle of class a static variables get initialized one and only once while class being loading into memory where as an instance variable get initialized each and every time the object is created.
Interview Capsule: In many interview asking that what is the min and max count of Initialization for static and instance variable?
Answer: The min count and max count is always one for the Static variable because it got initialized immorally when the class loaded inside the memory and persist in that value through its life cycle, but the min count for instance variable is always zero because if we don’t created any object for that class then the variable can not got initialized and max count is ‘n’, hear ‘n’ represent how many time we created the object that many time the instance variable got initialized.
- To consume the static member outside of the class we need class name whereas to access the instance member outside of the class we need object of that class.
Constant variable VS Read only Variable
- A variable where value can’t be modified once after declaration is know as constant variable where as in read only variable we can make separation in between declaration and initialization.
For eg: If I do like this,
- Const int x;
- X=9;
- Readonly int x;
- X=9;
Similar behavior in between constant variable and a static variable.
Like static variable initialize immediately when the class loads inside memory, in the same way constant variable got initialized only once at the class loading time.
Where as we have also similar behavior for readonly variable and instance variable.
- To declare constant variable we use ‘const’ keyword where as to declare readonly variable we use ‘readonly’ keyword.
Example:
- const float pi=3.14f;
- rreadonly int a=89;
- Or readonly int a;
- a=89;
Readonly
- Key Capsule: It’s important to remember is that the readonly variable can declared and initialized at one line or we separate it as well as we can initialize readonly variable through constructor of that class.
- Key Capsule: The value of readonly variable is constant for a single object that means the readonly variable value fix with in the life cycle of an object. So hear important part is to understand that if create more then one object then we can make change the readonly variable value by using particular object.
For e.g:
- Class Demo
- {
- readonly int a;
- readonly float pi;
- Demo(int a)
- {
- this.a = a;
- }
- Static void Main()
- {
- Demo obj1 = new Demo(8);
- Console.writeLIne(obj1.a)
- Obj1.pi = 3.141 f;
- Obj1.pi = 2.14 f;
- Console.writeLIne(obj1.pi);
- Demo obj2 = new Demo(10);
- Console.writeLine(obj2.a);
- Obj2.pi = 1.12 f;
- Obj2.pi = 6.90 f;
- Console.writeLIne(obj2.pi)
- }
- }
Output:
8, 3.141,10,1.12.
So in the example what we see is that readonly variable makes the value fixed for a single object, as many numbers of of object we created that many times we can re-initialize the readonly variable.
Memory Capsule for Static Class:
- Static class only contains static member.
- Inside static class we don’t have any parameterized constructor.
- Static class is a sealed class that means we can’t inherit any class form static class.
- Static constructor is the first block of code which executed before the Main method execution.
I hope this article will help your understanding of static, instance, readonly and constant variable.
If you are a fresher then I must suggest you please carefully read all the “Memory Capsule” and “KeyCapusle” which will help you a lot in your interview.
“Never accept more as your capability; take all your time to improve your knowledge and confidence so that without accepting more by default you get what you want”.
Read more articles on C# Programming: