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
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
- 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,
-
-
- public circle(int Radius)
- {
-
- this._Radius = Radius;
- }
lets Going to create another Method Actually Get calcualte area of circle Return
-
-
- 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();
- }
- }
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 .
- class circle
- {
- static float _PI =3.141F ; //Static Class Fields
- int _Radius;
-
- public circle(int Radius)
- {
-
- this._Radius = Radius;
- }
-
- public float CalcualateArea()
- {
- return circle._PI * this._Radius * this._Radius; //Refer to Static member we use name of the class
- }
-
- }
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
- 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
- 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.