-->

Thursday, April 4, 2013

What is Difference Between static member and non static member

What is Difference Between static member and non static member

Also known as static member or instance member

(1)calling
Static : Static member always call from class name.
NonStatic : NonStatic member always call from instance of the class.

Note: Those values who have not change during the entire program that values keep as a static 
Example:
In Circle class the pi=3.14 value always must constant so use pi as a static member

(2)declaration
NonStatic declaration :  float pi=3.14F;
Static declaration :  static float pi=3.14F;


(3)In case of Constructor
Static : We can't use access modifier with static constructor.
NonStatic: we use always public modifier with non static constructor.

Note: 
Static Constructor is called only once , no matter how many instances you create 
Static Constructors are called before instance constructors.


(4)In Case of memory
In instance member belongs to specific instance (object) of a class. If i create 3 objects of a class , i will have 3 sets of instance members in the memory , where as there will ever be only one copy of a static member , no matter how many instances of a class are created.

Read other related articles

Also read other articles

© Copyright 2013 Computer Programming | All Right Reserved