Friday, 15 February 2013

Static Modifier

This is used for declaring the members of the class as well as the class as static. As we aware that a class is a collection of members and those members are divided into two categories :-
  • Static Members
  • Non-static Members
The  members of a class which doesn't require the object of the class for initialization or execution are known as static members, whereas the members of class which requires object of class for initialization and execution are known as non static members.

Note:  Non static members can also be called as instance method because they are associated with an object or instance of class.

Static members vs non static variables
  • A variable that is declared using static modifier or a variable that is declared under any static block is a static variable rest of the other are non-static only.
         int x=100                  // non static
         static int y=200         //static
         static void  Main()
        { 
          int z=300;               //static
        } 
  • A static variables get initialized immediately once the execution of class starts and whereas a non-static variables get initialized on when the object of the class is created as well as each time the object is created.
  •    A static variable get initialized one and only one time in the life cycle of class whereas a non static variables mat be initialized for 0 or n times.

No comments:

Post a Comment