Amazon Interview Question
Software Engineer / Developerswell it has different meanings in different contexts..
when used with a member in a user-defined data structure such as a class, it specifies that the member is of that type itself rather than for every specific object of that class
when uses with a variable in a function, it retains its state between the function calls
for example:
void test(int current)
{
static int i; //initially will be zero if nothing is initailised
current = current + i;
print current;
}
int main()
{
for(int i=0;i<5;i++)
test(i);
}
the output should be 0 1 3 6 10
by declaring a method/variable static, we assign it to the entire class rather than any instance in specific.
- Anonymous November 08, 2008as a result, such variables/methods are loaded in memory even before a class is instantiated.