Static Keyword in Details

The static keyword can be used for any of 3 purposes.

1. For Data Members

It is a variable that belongs to the class. A single copy can be shared among all instances of a class. For the use of the static variables, creation of an instance is not required.

Access using "ClassName.VariableName", unlike instance variables accessed as "ObjectName.VariableName".

Static Data Member - > StaticDataMember.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace StaticKeyWordDemo.Classes

{

    public class StaticDataMember

    {

        private int StudId;

        private static int nextStudId = 1;

 

        public StaticDataMember()

        {

            StudId = nextStudId++;

        }

 

        public void PrintRecord()

        {

            Console.WriteLine("Student Id is {0}", StudId);

        }

 

    }

}

 

using StaticKeyWordDemo.Classes;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace StaticKeyWordDemo

{

    class StaticKeyword

    {       

        static void Main(string[] args)

        {

            StaticDataMember sd1 = new StaticDataMember();

            StaticDataMember sd2 = new StaticDataMember();

 

            //printing

            Console.WriteLine("*** Static Keyword Demo ***");

            sd1.PrintRecord();

            sd2.PrintRecord();

            Console.ReadKey();

        }

    }

}

OutPut

Static1.jpg

2. Static Method Member

Like any static member, a static method belongs to the class (not the object). They are accessed using "ClassName.MethodName". It is a class method. To use static methods, creation of an instance is not necessary. Static methods require an object reference to access other static data and methods. Static methods can not directly access data members and members, they must use an object reference.

Static Data Members - > StaticMethodMember.cs
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace StaticKeyWordDemo.Classes

{

    public class StaticMethodMember

    {

        private int EmpId;

        private static int nextEmpId = 100;

 

        public static void PrintNextEmp()

        {

            Console.WriteLine("Next Employee Id is {0}", nextEmpId);

        }

    }

}

 

using StaticKeyWordDemo.Classes;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace StaticKeyWordDemo

{

    class StaticKeyword

    {       

        static void Main(string[] args)

        {

            Console.WriteLine("*** Static Keyword Demo ***");

           

            StaticMethodMember.PrintNextEmp();

 

            Console.ReadKey();

        }

    }

}


Output

Static2.jpg

3. Static Constructor

A class can have only one static member. A static constructor cannot have a parameter. A static constructor cannot have any access specifies, not even private. It is used to initialize the static data members of the class. Regardless of the number of object creations, the static constructor is executed only once.

The static constructor is executed when the class is used or referenced for the very first time in the application. It cannot be invoked by the programmer explicitly.

Static Constructor - > StaticConstructor.cs
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace StaticKeyWordDemo.Classes

{

    public class StaticConstructor

    {

        private static int nextEmpId;

        private int EmpId;

        private string EmpName = "Krishn";

 

        static StaticConstructor()

        {

            nextEmpId = 100;

        }

 

        public StaticConstructor()

        {

            EmpId = nextEmpId++;

        }

 

        public StaticConstructor(string empName)

        {

            EmpId = nextEmpId++;

            this.EmpName = empName;

        }

 

        public void PrintInfo()

        {

            Console.WriteLine("EmpId : {0}", EmpId);

            Console.WriteLine("EmpName : {0}", EmpName);

        }

    }

}

 

using StaticKeyWordDemo.Classes;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace StaticKeyWordDemo

{

    class StaticKeyword

    {       

        static void Main(string[] args)

        {

            Console.WriteLine("*** Static Keyword Demo ***");

           

            StaticConstructor sc1 = new StaticConstructor();

            sc1.PrintInfo();

            Console.WriteLine();

            StaticConstructor sc2 = new StaticConstructor("Jeetendra");

            sc2.PrintInfo();

 

            Console.ReadKey();

        }

    }

}


OutPut

Static3.jpg

The first time during class load for execution, the nexEmpId is initialized to 100. After that, for every instance, a unique number stating from 100 is assigned to each individual StaticConstructor object as its empId.of StaticConstructor class created using either of the constructors, the nextEmpId is assigned to the instance field empId and then incremented. Thus this code generates.

The static constructor is called implicitly only once in the lifetime of the application. Subsequently only the default or parameterized constructor is called, depending on the way the object is created.


Similar Articles