What is Static Variable

Static Variable is also known as class variable. It is declared like a global variable. Static variable is called using class name. We cannot call static variable with object

As static variable is class variable, so only one copy of this variable will be created and used every time when we call.

Static variable is initialized before we use class or create class object. Therefore, we can say static variables always have a value. If user does not initialize it then it will be initialized with its default value like int with zero, reference variable with null etc. automatically.

Example of Static Variable

public class abc
    public static int x;

Calling for static variable

protected void Page_Load(object sender, EventArgs e)


In this example you can see that we have created x variable as a static variable and called it using class name instead of object.

Static variable cannot be called using object. When you will execute this code, you will get zero value in output as default value is initialized for static variable.

Difference between Static Variable and Non Static Variable