Understanding Static
When you want to define a
class member that will be used independently of any object of that class.
Normally, a class member must be accessed through an object of its class. But it
is possible to crate a member that can be used by itself, without reference to a
specific instance. To create such a member, precede its declaration with the
keyword Static, when a member is declared static, it can be accessed
before any objects of its class are created and without reference to any object.
You can declare both methods and variables to be static. The most common example
of static member is Main(), which is declared static because it
must be called by the operating system when your program begins.
Outside the class, to use a
static member, you must specify the name of its calls followed by the dot
operator.
For example:
Timer.count =10;
Here Timer is the name of
class and count is static member.
Variable declared as static
are, essentially, global variables. When objects of its class are declared, no
copy of a static variable is made. Instead all instead of class share the
same static variable. Static variable is initialized before its class is used.
If no explicit initializer is specified, it is initialized to Zero for
numeric type, null in the case of reference types, or false
for variable of type bool. Thus static variable always have a value.
Example
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
@static
{
class staticdemo
{
public static
int val = 10;
public static
int valdiv()
{
return val / 2;
}
}
class Program
{
static void
Main(string[] args)
{
Console.WriteLine("initial
value of static variable is " + staticdemo.val);
staticdemo.val = 100;
Console.WriteLine("static
variable value after reinitialized " +
staticdemo.val);
Console.WriteLine("after
method valdiv() called value " + staticdemo.valdiv());
Console.Read();
}
}
}
Output
Some important point that
apply to static method.
A static
method does not have a this reference. This is because a static
method does not execute relative to any object.
A static method can
directly call only other static methods of its class. It cannot directly
call an instance method of its class. The reason is that instance methods
operate on specific objects, but a static method is not called on an object.
Thus, on what object would the instance method operate?
A similar restriction
applies to static data. A static method can directly access only other
static data defined by its class. It cannot operate on an instance variable
of its class because there is no object to operate on.
Note: Above declared
point exception also occurs. Which are describing below?
It is important to
understand that a static method can call instance methods and access instance
variables of its class if it does of that class.
For example
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
static_exception
{
class staticexp
{
public int i;
public void
show()
{
this.i = 55;
Console.WriteLine("this
is non-static method");
}
public static
int a;
public static
void display(staticexp
obj)
{
obj.show();//
non static called from static method
obj.i = 200;
a = 100;
//this.a = 10; illigal because static
method does not have this refernce
}
}
class Program
{
static void
Main(string[] args)
{
staticexp obj = new
staticexp();
staticexp .display (obj);
Console.Read();
}
}
}
Output
good one sayta
ReplyDeleteNcee one .....keep in touch sayta..
ReplyDelete