StaticAs we are aware a class is a collection of members like variables, methods, Constructors, etc. These members will be of two different categories.
1) Non-static (or) Instance Methods
2) Static members
The members of a class that requires the object of a class for initialization or execution are known as Instance members whereas members of a class which does not require an object of a class for initialization or execution are known as static members.
Instance variables Vs Static Variables
1) A variable under a static block or a variable declared using static modifier are static variables, rest of all are instance variables only
Eg: - int x=100; }--> instance
static int y-100; }--> static
static void Main()
{ |
int z=100; |--> static
} |
2) The initialization of a static variable gets done if we execute the class in which the variable is declared whereas instance variable gets initialized after creating the object of class either within the class or in other classes also.
3) The minimum and a maximum number of times a static variable gets initialized will be one and only one time whereas in 'n' case of instance it will be '0' if no objects are created or 'n' if 'n' objects are created.
Note:-To access an instance member we use the object of the class whereas to access static members we use the name of the class.
We use Instance variables if at all we want any value that is specific to an object.
Eg:- Customer id and balance in our previous class Customer whereas we use a static variable if we want any value that should be common for the complete class.
Constant Variables
It is a variable that contains a fixed value that cannot be modified while declaring a constant at the time of declaration only we need to assign values to it. The behavior of a constant variable will be the same as the behavior of a Static variable. The only difference is static variables can be modified but constant variables cannot be modified.
ReadOnly Variables
These are very much similar to constants whose values cannot be modified after initialization but their behavior will be similar to Instance variables which are initialized when the object is created and also maintains a separate copy for each object.
A Constant value (Variable) is a fixed value for the complete class whereas the ReadOnly variable is a fixed value specified for each object.
Note: - A ReadOnly variable can be initialized either under constructor or at the time of declaration.
*Add a class “StatVars.cs” and write the following.
Static Methods Vs Instance Methods
A Method declared using static modifier is a static method. The rest of all are Instance objects while defining static methods make sure we are not consuming any nonstatic members of the class directly from static methods we need to consume them only using object of the class whereas the reverse is possible directly.
Note: - Non-Static members of a class cannot be consumed from static blocks of a class directly they need to be consumed using class objects.
*Add a class “StatMets.cs” and write the following code.
A constructor declared using static modifier is static constructor rests of all are Instance only.
A static constructor first block of code that gets executed under a class where an Instance constructor gets executed only if the object of the class is created and each time the object is created.
The static constructor of a class gets executed only if we execute the class in which it was defined whereas Instance constructor gets executed wherever and whenever we create the object of class in which it was defined.
Use static constructor for writing the code to perform an action when the execution of ‘class’ type whereas use Instance Constructor to perform an action that should be executed each time the object is created.
A static constructor cannot be parameterized because we never call it directly so sending a value to it will not possible but we can parameterize Instance Constructors.
Static Classes
These are introduced in C# 2.0, which can contain only static members in that. The object of these classes cannot be created. we call using the class name.
1) Non-static (or) Instance Methods
2) Static members
The members of a class that requires the object of a class for initialization or execution are known as Instance members whereas members of a class which does not require an object of a class for initialization or execution are known as static members.
Instance variables Vs Static Variables
1) A variable under a static block or a variable declared using static modifier are static variables, rest of all are instance variables only
Eg: - int x=100; }--> instance
static int y-100; }--> static
static void Main()
{ |
int z=100; |--> static
} |
2) The initialization of a static variable gets done if we execute the class in which the variable is declared whereas instance variable gets initialized after creating the object of class either within the class or in other classes also.
3) The minimum and a maximum number of times a static variable gets initialized will be one and only one time whereas in 'n' case of instance it will be '0' if no objects are created or 'n' if 'n' objects are created.
Note:-To access an instance member we use the object of the class whereas to access static members we use the name of the class.
We use Instance variables if at all we want any value that is specific to an object.
Eg:- Customer id and balance in our previous class Customer whereas we use a static variable if we want any value that should be common for the complete class.
Constant Variables
It is a variable that contains a fixed value that cannot be modified while declaring a constant at the time of declaration only we need to assign values to it. The behavior of a constant variable will be the same as the behavior of a Static variable. The only difference is static variables can be modified but constant variables cannot be modified.
ReadOnly Variables
These are very much similar to constants whose values cannot be modified after initialization but their behavior will be similar to Instance variables which are initialized when the object is created and also maintains a separate copy for each object.
A Constant value (Variable) is a fixed value for the complete class whereas the ReadOnly variable is a fixed value specified for each object.
Note: - A ReadOnly variable can be initialized either under constructor or at the time of declaration.
*Add a class “StatVars.cs” and write the following.
class StatVars
{
int x = 100; //Instance
static int y = 100; //Static
public const float pi = 3.14f; //Constant
public readonly bool flag; //ReadOnly
public StatVars(bool flag)
{
this.flag = flag;
}
static void Main()
{
Console.WriteLine(StatVars.y);
Console.WriteLine(StatVars.pi);
StatVars s1 = new StatVars(true);
StatVars s2 = new StatVars(false);
Console.WriteLine(s1.x + " " + s2.x);
Console.WriteLine(s2.flag + " " + s2.flag);
Console.ReadLine();
}
}
Static Methods Vs Instance Methods
A Method declared using static modifier is a static method. The rest of all are Instance objects while defining static methods make sure we are not consuming any nonstatic members of the class directly from static methods we need to consume them only using object of the class whereas the reverse is possible directly.
Note: - Non-Static members of a class cannot be consumed from static blocks of a class directly they need to be consumed using class objects.
*Add a class “StatMets.cs” and write the following code.
class StatMets
{
int x = 100;
static int y = 200;
static void Add()
{
StatMets obj = new StatMets();
Console.WriteLine(obj.x + y);
}
static void Main()
{
StatMets.Add();
Console.ReadLine();
}
}
Static Constructor Vs Instance ConstructorA constructor declared using static modifier is static constructor rests of all are Instance only.
A static constructor first block of code that gets executed under a class where an Instance constructor gets executed only if the object of the class is created and each time the object is created.
The static constructor of a class gets executed only if we execute the class in which it was defined whereas Instance constructor gets executed wherever and whenever we create the object of class in which it was defined.
Use static constructor for writing the code to perform an action when the execution of ‘class’ type whereas use Instance Constructor to perform an action that should be executed each time the object is created.
A static constructor cannot be parameterized because we never call it directly so sending a value to it will not possible but we can parameterize Instance Constructors.
class StatCon
{
static StatCon()
{
Console.WriteLine("Static Members");
}
StatCon()
{
Console.WriteLine("Instance Constructor");
}
static void Main()
{
Console.WriteLine("Main Method");
StatCon c1 = new StatCon();
StatCon c2 = new StatCon();
Console.ReadLine();
}
}
These are introduced in C# 2.0, which can contain only static members in that. The object of these classes cannot be created. we call using the class name.
No comments:
Post a Comment