Static Variables, Static Methods and Objects in C#

Static Variables and Static Methods

Each object has its own set of member variables and all the member variables have a scope.

If we want a variable to have the same value throughout all instances of the object then we can declare it as a static variable in our program. To manipulate and use the values of static variables we can also define a function as static.

The keyword "static" means that only one instance of a given variable exists for a class. Static variables are used to define constants because their values can be retrieved by invoking the class without creating an instance of it.

Static variables can be initialized outside the member function or class definition. The following code is an example of a static variable, which shows the declaration and initialization of a static variable.

  1. using System;  
  2. namespace staticexample  
  3. {  
  4.     class Program  
  5.     {  
  6.         public static int i;  
  7.         public static void display()  
  8.         {  
  9.             i=10;  
  10.             Console.WriteLine(i);  
  11.         }  
  12.         public void demo()  
  13.         {  
  14.             int j=20;  
  15.             Console.WriteLine(j);  
  16.         }  
  19.         static void Main(string[] args)  
  20.         {  
  21.             Program obj = new Program();  
  22.             Program.display();  
  23.             obj.demo();  
  24.             Console.Read();  
  25.         }  
  26.     }  
  27. }
Here we have not used an instance of the class for the static method display(). However, for the nonstatic demo() method we need the instance of the class to call it. This is how to call static and non-static methods.


Unlike other member variables, only one copy of the static variable exists in memory for all the objects of that class. Therefore, all objects share one copy of the static variable in memory.


An object is an instance of a class. We create objects to access member variables and member functions of a class. To use the member of a class, we need to create an object of the class. Objects interact with each other by ing messages and by responding to the received messages. In C# , the task of ing messages can be done using member functions.

  1. class Program  
  2. {  
  3.     static int x, y;  
  4.     void display()  
  5.     {  
  6.         int result = x + y;  
  7.         Console.WriteLine("the result is " + result);  
  8.     }  
  9.    static void Main(string[] args)  
  10.    {  
  11.         Console.WriteLine("enter the numbers");  
  12.         x=Convert.ToInt16(Console.ReadLine());  
  13.         y=Convert.ToInt16(Console.ReadLine());  
  14.         Program obj=new Program();  
  15.         obj.display();  
  16.         Console.ReadLine();  
  17. }
In the code above we have declared a class named "Program". To create an object of the class named "obj", the following code is given in the Main() method:
  1. Program obj=new Program();
The member function of the class is accessed through an object of the class using the "." operator, as shown in the following example:


The code above is used to access the member function display(). This means that the object obj of the class Program has invoked the function display(). This function calculates the result and displays it on the console.

Recommended Free Ebook
Similar Articles