Monday, 5 January 2015

Static Class, Static Methods, Static Members and Static Constructors


Static Class

  • A static class is never instantiated.
  • We can access members and functions directly with the class name.
  • A static class cannot have non-static members.
  • All methods,fields and properties in it must be static.

 Example :

using System;

class Program
{
     static void Main()
       {
// Cannot declare a variable of type Phone.
// Program is a regular class so you can create it.
   Program program = new Program();

// You can call static methods inside a static class.
        Phone._right= true;
        Phone.micromax();
       }
}


static class

Phone
{
// Cannot declare instance members in a static class!
// int _test;
// This is right.
   public static bool _right;

// Can only have static methods in static classes.
   public static void micromax()
    {
      Console.WriteLine("This is a micromax phone");
    }
}

Output

This is a micromax phone


Static Members
  • A non-static class can contain static methods, fields, properties, or events.
  • Static members of the non static class can be called with the class name directly without creating object.
  • The static member always called by the class name.
  •  Static member is shared among all instances of the class. So if an instance changes value of the static field,then all the instance of this class will get the changed value.
  • For the non static class, two common uses of static fields are to keep a count of the number of objects that have been instantiated, or to store a value that must be shared among all instances.
  • Static methods can be overloaded but not overridden, because they belong to the class, and not to any instance of the class.
 Example :
 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Static
{
  class Program
   {
    static void Main(string[] args)
     {
      AutomobileCompany.DriveFast();
      int i = AutomobileCompany.NumberOfTyres;
     }

   public  class AutomobileCompany
    {
      public static int NumberOfTyres = 4;
      public static int SizeOfGasCylinder
       {
         get
            {
              return 15;
            }
       }
   public static void DriveFast() { }

    }

   }
}




Static Methods

  • A Static method can be called with the class name directly without creating instance of the non static class.
  • Static methods can be overloaded but not overridden, because they belong to the class, and not to any instance of the class.
 Example
 

class

Mathematics
{

//Do something

   public static double CubeRoot(double d)
     {
        //Do something
     } 
} 
 

 

Static Constructor

  • A static constructor is used to initialize static data.
  • A Static constructor is used to perform a particular action that needs to performed once only
  • A static constructor does not have access modifiers and parameters.
  • A static constructor called only once when first object of the class is created or any static members are referenced.
  • A static constructor cannot called directly while creating a object.
  • User cannot changed member field values of the static constructor.
Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Static
{
    class Program
    {
        static void Main(string[] args)
        {
            AutomobileCompany obj = new AutomobileCompany();
            obj.DriveFast();
            AutomobileCompany obj2 = new AutomobileCompany();
            obj2.DriveFast();
            int i = AutomobileCompany.NumberOfTyres;
           
        }

        public  class AutomobileCompany
        {
            public static int NumberOfTyres = 4;
          
            static AutomobileCompany()//it is a static constructor called only once when first object is created
            {
                NumberOfTyres = 5;
            }
        
            public static int SizeOfGasCylinder
            {
                get
                {
                    return 15;
                }
            }
            public void DriveFast() { }
          
        }

    }
}

1 comment:

  1. Hi Guys,

    If you have any questions regarding the blog please write to me.

    ReplyDelete