Use of static Keyword


Static Keyword can be used with
1. Classes
2. Members
3. Methods/Properties/Events

1. Classes

When a class is declared as static it can have only static members
It can not be instantiated (It is sealed class).

Example to illustrate static class

    static class JobLocation
    {
        public static string GetJobLocation() 
             { 
                   return "H.No. Abc Test Street"
             }
    }


2. Members
       Static variables belongs to class.
       Non Static members belong to instance i.e. object of a class.
       A class can have static as well as non Static members.
       When an instance of a class is created it will have a separate copy of non static Members but a single copy of static member which belongs to class and not to the instance of that class.
       It is not possible to use this keyword with static members.

    class Employee
    {
        int employeeID;
        static string companyName;
    }


3. Methods/Properties/Events
Static methods can only use static Members. 
A class can have static methods as well as non Static methods.

class Employee
    {
        int employeeID;
        static string companyName;
      
        public static void EmployeeData()
        {
            // we can access companyName here as it is static.
            // we can not access employeeID as it is non static.

            companyName = "Test Company";
        }

        public void EmployeeDetails()
        {
            EmployeeData();
            employeeID = 2;
        }

        public void PrintEmployeeDetails()
        {
            Console.WriteLine("Comapny Name is " + companyName);
            Console.WriteLine("EmployeeID is " + employeeID);

        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Employee emp = new Employee();
            emp.EmployeeDetails();
            emp.PrintEmployeeDetails();
            Console.Read();

        }
    }


And to directly call static method we use class and not the instance

    class Employee
    {
        int employeeID;
        static string companyName;
        public static void EmployeeData()
        {
            // we can access companyName here as it is static.
            // we can not access employeeID as it is non static.

            companyName = "Test Company";
        }
        public void EmployeeDetails()
        {
            employeeID = 2;
        }
        public void PrintEmployeeDetails()
        {
            Console.WriteLine("Comapny Name is " + companyName);
            Console.WriteLine("EmployeeID is " + employeeID);

        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Employee.EmployeeData();
            Employee emp = new Employee();
            emp.EmployeeDetails();
            emp.PrintEmployeeDetails();
            Console.Read();

        }
    }

4. Static Constructors

Static Constructors are used to initialize static Data.
Static Constructors can not have access specifier ( Not even private) and cannot be overloaded.

class Employee
    {
        int employeeID;
        static string companyName;
        static Employee()
        {
            // we can access companyName here as it is static.
            // we can not access employeeID as it is non static.
            Console.WriteLine("In Employee Class static constructor");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {

            Employee emp = new Employee();
            Console.Read();

        }
    }

No comments:

Post a Comment