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
- Static fields
- Instance fields
- Difference Between Static and Instance Members
- Static Constructor
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
- float _PI =3.141F ; //Instance Class Fields
- 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,
- //Constrcutor Create For Initialize for class Filds
- public circle(int Radius)
- {
- this._Radius = Radius;
- }
lets Going to create another Method Actually Get calcualte area of circle Return
- //Create method calculate Radius value. called my method name CalcualateArea
- public float CalcualateArea()
- {
- return this._PI * this._Radius * this._Radius;
- }
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,
- class Program
- {
- static void Main(string[] args)
- {
- circle c1 = new circle(5);
- float area1 = c1.CalcualateArea();
- Console.WriteLine("Area={0}", area1);
- circle c2 = new circle(6);
- float area2 = c2.CalcualateArea();
- Console.WriteLine("Area={0}", area2);
- Console.ReadLine();
- }
- }

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 .
- class circle
- {
- static float _PI =3.141F ; //Static Class Fields
- int _Radius;
- //Constructor
- public circle(int Radius)
- {
- this._Radius = Radius;
- }
- //method Name
- public float CalcualateArea()
- {
- return circle._PI * this._Radius * this._Radius; //Refer to Static member we use name of the class
- }
- }

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
- using System;
- class circle
- {
- static float _PI =3.141F ;
- int _Radius;
- static circle()
- {
- Console.WriteLine("My Static constructor");
- circle._PI = 3.141F;
- }
- public circle(int Radius)
- {
- Console.WriteLine("My Instance constructor");
- this._Radius = Radius;
- }
- public float CalcualateArea()
- {
- return circle._PI * this._Radius * this._Radius;
- }
- }
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- circle c1 = new circle(5);
- float area1 = c1.CalcualateArea();
- Console.WriteLine("Area={0}", area1);
- circle c2 = new circle(6);
- float area2 = c2.CalcualateArea();
- Console.WriteLine("Area={0}", area2);
- Console.ReadLine();
- }
- }
- }
Result Will be like This,
between static and Instance Constructor
between static and Instance Constructor
- When Class members Include static modifier the member is called static member.
- When no static modifier present is present member is called instance member or non static instance member .
- 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.
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.

Yogi SPosted Mar 20, 2017, 6:07 AM
Nicely explained. Good.
Anu VPosted Mar 20, 2017, 5:12 AM
Nice article.. Thanks for sharing..