Static is a keyword while defining class and class members.
When we declare any class and class members as static, for accessing those
variables do not need any object we can access these variables only by using class
name. Static members shared among all objects.
You can use static class if data or method declared in the
class does not depend on behavior of object.
Example
using System;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
//Employee e = new Employee("John\n");
Console.Write(Employee.GetEmployee());
Console.ReadLine();
}
}
static class Employee
{
static string
EmployeeName = "John";
static Employee()
{
Console.Write("Default
Constructor called\n");
}
public static string GetEmployee()
{
return EmployeeName;
}
}
}
See in the above example we can access static methods by
using class name itself. When program start and when we use class first time
then constructor gets executed. Means the output of above program is
Output:
Default Constructor called
John
If we comment the line where we call the GetEmployee()
Method then static constructor not get called.
We can use Default and static constructor in single class
see example below:
using System;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
//Employee e = new Employee("John\n");
Employee Emp = new
Employee();
Console.Write(Employee.GetEmployee());
Console.ReadLine();
}
}
class Employee
{
static string
EmployeeName = "John\n";
static Employee()
{
Console.Write("Static
Constructor called\n");
}
public Employee()
{
Console.Write("Default
Constructor called\n");
}
public static string GetEmployee()
{
return EmployeeName;
}
}
}
When we use static and default constructor in same class
then execution sequence is static constructor is executed first and then
default constructor. And Static constructor is executed only once for all
object.
Output:
Static Constructor called
Default Constructor called
John
Key Points:
1.
Static class contains only static members and
methods.
2.
Static class cannot contain Destructors.
3.
Static constructors must be parameter less.
4.
We can Access static methods by using class name
itself.
5.
When first use of static class that time static
constructor is called.
6.
We can use Default and static constructor in
single class.
7.
Static members shared among all objects.
8.
Static constructor calls first and after that
remaining all constructors.
Some Examples:
1.
using System;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
//Employee e = new Employee("John\n");
Employee Emp = new
Employee();
Console.Write(Employee.GetEmployee());
Console.ReadLine();
}
}
class Employee
{
static string
EmployeeName = "John\n";
static Employee()
{
EmployeeName = "Royal";
Console.Write("Static
Constructor called\n");
}
public Employee()
{
Console.Write("Default
Constructor called\n");
}
public static string GetEmployee()
{
return EmployeeName;
}
}
}
Output:
Static Constructor called
Default Constructor called
John
2.
using System;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
//Employee e = new Employee("John\n");
Employee Emp = new
Employee();
Console.Write(Employee.GetEmployee());
Console.ReadLine();
}
}
class Employee
{
static int
EmployeeName = 10;
static Employee()
{
EmployeeName++;
Console.Write("Static
Constructor called\n");
}
public Employee()
{
EmployeeName++;
Console.Write("Default
Constructor called\n");
}
public static int GetEmployee()
{
return EmployeeName;
}
}
}
Output:
Static Constructor called
Default Constructor called
12
3.
using System;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
//Employee e = new Employee("John\n");
Employee Emp = new
Employee();
Console.Write(Emp.GetEmployee() + "\n");
Console.Write("Emp
Object output End\n");
Employee Emp2 = new
Employee();
Console.Write(Emp2.GetEmployee() + "\n");
Console.Write("Emp2
Object output End\n");
Console.ReadLine();
}
}
class Employee
{
static int
EmployeeName = 10;
static Employee()
{
EmployeeName++;
Console.Write("Static
Constructor called\n");
}
public Employee()
{
EmployeeName++;
Console.Write("Default
Constructor called\n");
}
public int
GetEmployee()
{
return EmployeeName;
}
}
}
Ouput:
Static Constructor called
Default Constructor called
12
Emp Object output End
Default Constructor called
13
Emp2 Object output End
0 Comments