Wednesday, July 2, 2014

Static Variable Vs Instance Variable

Instance Variable vs static variable

A variable declared using static modifier or a declare under static block were known as “static variable” rest of all are instance variables

 Eg: 
int x=100;
Static int y=200;
Static void Main()
{
Int z=300;
}

Note: here the instance variable gets initialized whenever the object of class gets created, so the minimum or maximum no of times, it gets initialized will be zero or N.

Whereas as a static variable get initialized once the execution of a class starts. so the minimum and the maximum no of times it gets initialized will be one and only one.

Use instance variable if we want the variable accessible only to the single object

Use static variable if we want the variable to accessible to all objects.

Statvar.cs
Class statvars
{
int x=100;
static int y=200;
static void Main()
{
Console.writeline(statvar.y);
Statvar sv=new statvar();
Console.writeline(sv.x);
Console.readline();

}

No comments:

Post a Comment

Thank you for visiting my blog

Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...