Use the static
modifier to declare a static member, which belongs to the type itself rather than to a specific object
A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new operator to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself. For example, if you have a static class that is named UtilityClass
that has a public static method named MethodA
, you call the method as shown in the following example:
UtilityClass.MethodA();
- Static classes can not be instantiatied
- All members must be declared static inside a static class
- Can have a constructor and that too would be declared static
static class can have static constructor, and the use of this constructor is initialization of static member.
static class Employee1 { static int EmpNo; static Employee1() { EmpNo = 10; // perform initialization here } public static void Add() { } public static void Add1() { } }
and static constructor get called only once when you have access any type member of static class with class name Class1
Suppose you are accessing the first EmployeeName field then constructor get called this time, after that it will not get called, even if you will access same type member.
Employee1.EmployeeName = "kumod"; Employee1.Add(); Employee1.Add();
Comments
Post a Comment