Explain About Static Fields And Instance Fields

Introduction

In this Article We will Discuss about how to use static Class Fields ,Instance Fields and Default Constructor ,Difference Between static and Instance members Fields. lets say I will Explain About Following Topic . i have Listed  Topics below 
  1. Static fields
  2. Instance fields
  3. Difference Between Static and Instance  Members 
  4. Static Constructor  
Instance Class Members

I create  Simple Class . i called class Name Circle  All of Now Circle Class  Have Two Fields  one of the _PI fields and Radious     Fields ._BI  acutally float let initalize  value  3.141 . Let Created  another fields Name Radius integer datatype.   that Two Fields Dont have Static Keyword  in front of them that means  this is called Instance of class. this fields are instance Fields or  Non Static Fields. Class member like Fields and Methods,Propertise etc all of these class member fields  have Types Class Fields  Either  
  •   Instance Fields
  •   static Fields  
  1. float _PI =3.141F ;  //Instance Class Fields 
  2. int _Radius;         //Instance class 
Instance Class fieds We know that have Constructor has   Same Name of the Class. Constructor Dont have Rerurn type  but it Take parameter. Constructor  we  use Initalize class Fields . in my Code Use Constructor to initalize Radius,  
  1. //Constrcutor Create For Initialize for  class Filds   
  2.   
  3.      public  circle(int Radius)  
  4.         {  
  5.   
  6.         this._Radius = Radius;  
  7.         }  
lets Going  to create another Method Actually Get calcualte  area of circle Return 
  1. //Create method calculate  Radius  value. called my method name CalcualateArea    
  2.   
  3.    public float CalcualateArea()  
  4.    {  
  5.        return this._PI * this._Radius * this._Radius;  
  6.    }  
CalcualateArea This Method Going To create Calculate area Of Circle and Return  the Area of value  . Now  we this Class make going To create object of this  class. How do that .we have  use NEW keyword to create Instance of Class  object,
  1. class Program  
  2.    {  
  3.        static void Main(string[] args)  
  4.        {  
  5.            circle c1 = new circle(5);   
  6.            float area1 = c1.CalcualateArea();  
  7.            Console.WriteLine("Area={0}", area1);  
  8.   
  9.   
  10.            circle c2 = new circle(6);  
  11.            float area2 = c2.CalcualateArea();  
  12.            Console.WriteLine("Area={0}", area2);  
  13.            Console.ReadLine();  
  14.        }  
  15.    }  
Above  we called constructor  and  pass Constructor Value to Radius Fields . i have create Two  Objects  C1,and C2   .we can pass constructor C1 object Value Radius  5. C2 object  value Radius 6. untill now we created now we create Two circle  Obects .No matter when Create one Circle or thousands Circle  _PI value  always be Constantly.  if Both _PI and _Radius both instance Fields Both  C1 will  have Copy of this  _PI=3.141F and _Radius=5   Fields.   same as other object C2 will  have copy of this fields _PI and _Radius  .keep in mind if i create thousands Time object Each Time this Two  Fields  will reflected Each  object. if _PI Static  fields  and _Radius  and instance Fields  _PI only One copy of the File it will share all the object  that you crated .Now complile the Code Two fields instance Fields now output Will be like this,



Static Class Members Fields

In this moment i will change _PI fields Static fields  . one important  point if class member fields have static keyword in front of theme it is called static  keyword .In  this moment i make _PI static  . _PI fields will become Static . we can not refer  static Fields with this keyword .
 
 
 
This keyword we use to Refer instance of class   Object. if we want  refer static Class fields  we use   insted this We use  to call staic fields  to Use  class name.my class name Circle I used  calss  name  to call static Fields .
  1. class circle  
  2. {  
  3.     static float _PI =3.141F ;  //Static Class Fields
  4.     int _Radius;  
  5.       //Constructor  
  6.      public  circle(int Radius)  
  7.         {  
  8.   
  9.         this._Radius = Radius;  
  10.         }  
  11.     //method Name   
  12.     public float CalcualateArea()  
  13.     {  
  14.         return circle._PI * this._Radius * this._Radius;  //Refer to  Static member   we use name of the class
  15.     }  
  16.   
  17. }  

another thing constructor can not be static constructor  _Radius   instance fields initalize this  fields we use instance of constructor .if  we make static fields  initalize  use static construcor  it can not have access modifier .  look at this  trying to do that  try to build the solution it will show Error Like  this


Static Constructor

Access modifers  Not have Static Constructor .if i remove Access modifier  instance of  constructor it will  become private  out side class  we can access only  access inside class only. Let  show  error Screen shot,

 
Initalize create instance Circle class in main class  you need pass constructor value which is initalize Constructor . when create class  object Constructor automatically call.thats why constructor should be public.static constructor Dont  have access  modifier .why we use static constructor to initalize static fields. Static Constructor called Before itself Instance Constructor .static  constructor used to initalize  the static fields  in  a class.   you should declare static keyword in front of constructor name .static constructor only once called .static constructor before instance constructor .let explain the code 
  1. using System;  
  2. class circle  
  3. {  
  4.     static float _PI =3.141F ;  
  5.     int _Radius;  
  6.   
  7.     static circle()  
  8.     {  
  9.         Console.WriteLine("My Static constructor");  
  10.         circle._PI = 3.141F;  
  11.      }  
  12.         
  13.   public circle(int Radius)  
  14.         {  
  15.         Console.WriteLine("My Instance  constructor");  
  16.   
  17.         this._Radius = Radius;  
  18.         }  
  19.      
  20.     public float CalcualateArea()  
  21.     {  
  22.         return circle._PI * this._Radius * this._Radius;  
  23.     }  
  24. }  
  25. namespace ConsoleApplication1  
  26. {  
  27.     class Program  
  28.     {  
  29.         static void Main(string[] args)  
  30.         {  
  31.             circle c1 = new circle(5);   
  32.             float area1 = c1.CalcualateArea();  
  33.             Console.WriteLine("Area={0}", area1);  
  34.             circle c2 = new circle(6);  
  35.             float area2 = c2.CalcualateArea();  
  36.             Console.WriteLine("Area={0}", area2);  
  37.             Console.ReadLine();  
  38.         }  
  39.     }  
  40. }  
Result Will be like This,

 

between  static and Instance Constructor 
  1. When Class members Include static modifier   the member is called static member.
  2. When no static modifier present is present member is called   instance member or non static instance member .
  3. Static member are invoked using class name, instance member  are invoked  using instances  of the class.

Conculsion

I hope you all Understand How to instance of class fields and static fields   also discussed  how to create static  constructor  and difference static and instance construtor. please share all your feedback and comments to improve my future article.