• Now Online : 30
  • admin@codemyne.net


Working With Static Concepts:
  • Static is a keyword.
  • Static can be used with variables, constructors, methods, classes.
  • Instance variables will be created seperately for every object.
  • Static variables will be created only once while class is loading into the memory, Hence static variables are also called as class variables
  • Static variables are sharable by all the objects.
  • If static variable is public, then it can be accessed directly with ClassName.StaticVariable.
  • Static constructor will be executed only once, while class is loading into the memory.
  • Static constructor is capable to access only static variables.
  • Static methods need to be called with ClassName.MethodName()
class Test
    public void print();
    public static void display();            

Test t = new Test(); -----> possible
t.print(); ------>possible
t.display(); ----->not possible
Test.print(); ----->not possible
Test.display(); ------>possible
Whenever a class contains all static methods, then it is recommended to declare the class as static. Static classes are not instantiatable means not possible to create an object. Following is the example for static variables and methods.

class Test
    private int i;    //instance variable
    private static int s;   //static variable
    static Test()   //static constructor
    public Test()  //contructor
        i = i + 1;
        s = s + 1;
        MessageBox.Show(i + " " + s);
private void button1_Click(object sender, EventArgs e)
    Test t1 = new Test();
    Test t2 = new Test();
    Test t3 = new Test();

Comments/Suggestions are invited. Happy coding......!

Comments Post a Comment