Why Static?
A static modifier is used when it is necessary that class has a member that is called independently of instantiation. A static member is called from a class, without the class having been instantiated. Although the obvious example is the Main() method. Utility classes commonly are used to gain access to static functionality, saving the overhead of instantiating of types.(refer to the technique as used in the .NET Framework, i.e System.Math).
When Static Constructor Are Invoked?
Static constructors are invoked automatically when one of the following happens;
→ When an object of a type is created.
→ A static member (methods or properties) of the class is invoked.
1) The static constructor for a type executes before any instance of this type is created.
2) The static constructor for a type executes before any of the static members for this type are referenced.
3) The static constructor for a type executes after the static field initializes (if any) for this type.
4) The static constructor for a type executes at most one time during a single program instantiation.
5) A static constructor should not have parameters or have access modifiers.
6) A static constructor is called automatically to initialize a type before the first object is created or any static members are referenced.
7) A static constructor can't be called directly.
8) The user (programmer) has no control over the static constructor's execution in the program.
9) A typical use of static constructors is when a type is using a log file and the constructor is used to write entries to this file.
Rules for using static keyword:
Rule#1. A static type must contain the static keyword in the type declaration.
Rule#2. A method should be static, like the method declaration must contain the static keyword.
Rule#3. Constructor must be static because a static type doesn't allow an instance constructor.
Rule#4. Constructor must not contain an access modifier.
Rule#5. Static class can't implement an interface.
Rule#6. Static class can't be a base class i.e. it cannot be derived from.
Basically, creating a static class is same as creating a class that contains private constructor and static members only. Classes that are declare with 'static' modifier are sealed and cannot be inherited. They cannot also inherit from any class except Object. Static classes contain only static constructor and cannot contain an instance constructor. Non-static classes can define a static constructor if the class contains static members and the programmer wants to initialize static members in this constructor.
A non-static class can have static properties, static methods,static fields or events. The static members are callable on a class even when no instance of the class that contains these static members, has been created. When we want to access the static members, we use the full class name with a dot sign and a members name. We have no need to create a instance of the class to access the static members. Only one copy of a static member exists, regardless of how many objects of the class are created. Static properties and static methods cannot access non-static events and fields in their containing types, and they cannot access an instance variable of any object of a type unless it is explicitly passed in a method parameter.
It is better to declare a non-static class with some static members, than to declare an whole class as static. There are mainly two common uses of static fields are to keep a count of the number of objects that have been instantiated, or to store a value that must be shared among all instances.
Static methods can overloaded but can't overridden, because they belong to the type, and not to any object of the type.
A field cannot be declared as static const, a const field is basically static in its nature. It belongs to the class, not to instances of the class. Therefore, const fields can be accessed by using the same TypeName.MembersName notation that is used for static members. And no object instance is required. C# does not support static local variables (variables that are declared in method's scope).
Following example explain the use of static key word in C#.NET:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("***********Output**********");
//Error1: Can not declare a variable of static type
//Error2: Can not create a instance of static class
//MyStaticClass objMyStaticClass = new MyStaticClass();
Console.WriteLine(MyStaticClass.B);
MyStaticClass.B = 45;
Console.WriteLine(MyStaticClass.B);
MyNormalClass
objMyNormalClass = new MyNormalClass();
objMyNormalClass.MyInstanceMethod();
Console.ReadKey();
}
}
static class MyStaticClass
{
//Error: Static classes can not have instance constructor
//public MyStaticClass()
//{
//}
static int A;
//Error: Access modifiers are not allowed on static
constructors
//public static MyStaticClass()
//{
//}
public static int B = 9;
//Error: Can not declare instance memebers in static class
//int C;
static MyStaticClass()
{
int t;
t = 78;
//static constructor can also initialized the instance
member or non static fields if its own
Console.WriteLine("This is from static constructor of MyStaticClass =
" + t);
}
//Error: Method must have a return type. Means we cann't
define a static constructor with paramaters
//static MyNormalClass(int a)
//{
//}
}
//This is Normal Class And it can aslo contain a static
members as well
public class MyNormalClass
{
int a;
static int c;
public MyNormalClass()
{
//Instance Constructor/Method can also initialized the
static memebers
c = 12;
Console.WriteLine("This is from Instace Constructor of MyNomralClass
and = " + c);
//Error: The modifier 'static' is not valid for this item
//static int b;
}
//Access modifiers are not allowed on static consturctors
//public static MyNormalClass()
//{
//}
static MyNormalClass()
{
//static constructor can also initialized the instance
member or non static fields if its own
int d;
d = 5;
Console.WriteLine("This is instance memeber initialize in static
constructor of MyNormalClass = " + d);
}
public void MyInstanceMethod()
{
c =
3423;
int y = 567;
Console.WriteLine("This is from MyInstanceMethod of MyNormalClass =
" + c + ",
" + y);
}
}
Difference between Static and Non static Constructor:-
There are some differences in static and non static constructor which is given below:-
• Static constructor is called automatically by CLR when class is loaded in first time in memory and instance constructor (non static constructor) is called whenever the object of the class is created.
• We can pass parameter in non static constructor but not to the static constructor.
• There can be only one static constructor and more than one non static constructor within the class.
• We can not use any access specifier in static constructors but can use within non static constructors.
(Practice Makes a Man Perfect)
No comments:
Post a Comment