HI friends...
Can any body give reason / logic behind static varable lifetime through out the process execution.
i.e how does compiler maintains the static varable life through out the process execution ?
example
#include<stdio.h>
void func1( );
main()
{
.......
for (i=0;i<5;i++)
func1( );
.........
return 0;
}
finc1( )
{
.......
static int i=0; /* next time the control comes here this statement has no effect on "i" why n how ?*/
printf("Static int i=%d\n",i++);
.......
}
the o/p is 0 to 4...
I will be thankfull for replies....
The meaning of static in C++ and in java is not exactly the same.
1.)When modifying a variable, the static keyword specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends) and initializes it to 0 unless another value is specified.
2.)When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared).A variable declared static in a function retains its state between calls to that function.
3.)When modifying a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all instances of the class. (This is also true for Java).
4.)When modifying a member function in a class declaration, the static keyword specifies that the function accesses only static members.(Also true for Java).
Static data members of classes must be initialized at file scope.
For a more detailed discussion of static keyword in java, have a look the
Java classes page on this site page.