Wednesday, July 22, 2015

Story of 'static'

Now I will tell you the story of 'static' Keyword:


static Keyword can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types other than classes

Static Data member:
  • -      Static data members accessible without creating any Object for the Class.
  • -      Create by ‘static’ keyword.
  • -      Static data member’s memory will be allocated when the class is used for the  1st tme in the main().
  • -      Static data members memory will not be allocated as part of the object.
  • -      Accessible in Static() & Non-Static ().

Static not Fix the Value, its Fix the Memory for 1st Time
Static data members are used to store the ‘common data’ that belong to all the object.
Syntax:
            AccessModifier static DataType VariableName;

Static Method():
  • -      Static Method() Accessible without creating any Object for the Class
  • -      Static Method() are accessible with Class Name.
  • -      Static Method() can access static data members Only; Can’t access non-      Static Method() or Data Members.
  • -      Static Method() are used to manipulations on static data members only.
  • -      Static Method() are declared as ‘static ’ keyword.


Syntax::
AccessModifier static ReturnType MethodName (arg1, arg2…)
{
            --------------
}

Static Constructor: 2 Types – Static & Non-Static constructor.
Used to initialize only static data members.
Static constructor is automatically call when the class name is used in main() for the 1st time.
Static constructor can initialise only static data members; can’t initials non-static data members.
Maximum can only one static constructor can be written in a class.
No Argument; No Return Value.
Syntax::
Static ClassName()
{
Static data members here………….
}

Static Class:
Static Class contain Only static-member; they can’t contain any non-static data member.
I can create Object for Static Class.
Static Classes avoided the confusion for the other programmer that there are not-static data member present on it.

Syntax::
Static class    ClassName
{
            ONLY staic data member;
}

Syntax to call Static Data Member:
ClassName.DataMember;
Syntax: to call static method:
ClassName.MethodName();
Syntax: to Call non-static data member:
ObjectName.DataMemberName;
Syntax: to call Non-static Constructor:
New ClassName();
Syntax: to Call non-static method:
Objectname.MethodName();

Program: -
using System;
namespace StaticDataMember  // Static not Fix the Vallue, its Fix the Memory for 1st Time
{
    //
    //If the static keyword is applied to a class, all the members of the class 'must be static' --
    static class Class1
    {
        // Static Data Member   -
       public  static int Var1=999;
       //public char ch = 's'; ----Instance member can't exist in Static Class

        //Static Method -
       public  static int Increment()
       {
           return Var1+1;
       }

        // Static Constructor   -
        static Class1()
        {
           // Var1 = Var1 + 1;
            Console.WriteLine("Woooooooooh. I am STATIC in Constructor,                                              I Called First");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {  
           // Console.WriteLine(Class1.Var1);
           Class1.Increment();
            int temp = Class1.Increment();
            Console.WriteLine(temp);

            Console.ReadKey();
        }
    }
}


No comments:

Post a Comment