Working with Static Keyword

Class Circle


Private int x, y, z;

Private Static double pi = 3.14;.


Static is a keyword.

Static keyword can be used with fields, constructors, methods and classes.

Static Fields will be created only once while class is loaded in to the memory.

Instance Variables will be created separately with every object.

Static variables creates common memory which is sharable by all the objects.

If Static variable is public, then it can be accessed directly with class name But NOT WITH OBJECTS.


Class Test


Public int I;

Public Static int S;


Test I = 10; wrong

Test s =20; correct

Test t = new Test ()

  1. I = 10; correct

t.s = 20;wrong


Static constructor can access only static variables.

Static constructor will be Executed only once, While class is loading in to the memory.


Example on static variables and static constructor

Open windows forms Application Project arrrowarrrowPlace a button arrrowCode in GD

Class Test


Private int I;

Private Static int s;

Static Test ()// Static constructor


S= 0;


Public Test ()//Normal Constructor


I = I +1;

S = s+1;

Message Box>show  (I + “” +s);




Code for button 1_ click


Test t1 = new Test ();

Test t2 = new Test ();

Test t3 = new Test ();




T1  I =0

I=0 +1

I =1



I          s

1         1



T2   I =0

I = 0+1

I =1

S =s +1

S =I +1

S= 2


I     s

I     2                t2


I     s

I     3                  t3




Class JA  (Joint account)


Private string Name ()




Static Method and Static Classes


Class Test


Public Static void P1 (){}

Public Static void P2 (){}



Static method can access only Static data.

Static method need to be called with class name.





When a class contains all static methods only. Then recommended to declare that class as static.

Static class are not intangible (creation of object is not allowed) 


Example on static method and static classes

Open windows Application Project àPlace a text box and a button



arrrowIn this program a predefined class called as process is  required.

arrrowProcess class is a  part of System. Diagnostics namespace.

arrrowIn order to use process classes, this namespace need to be imported with the help of “using”  keyword.

arrrowCode in GD

Static classes software


Public static void open (string s)


Process. start (s);


}//software class


arrrowcode for Button 1-cilck


Software. open (text Box1.text);