Gday,
Just wondering what the static statement means in C++ and Java?
ie. public static void JavaMethod() or
static int variable;
I think part of it is personal preference in terms of how you code. Using a static in the function or a global outside are really the same thing. From a point of view for reading code I find it better to keep the variable as close as needed (local) to what your coding - this can make it easier to read other peoples code.
I am not sure what/if the performance hit would be between global and static local variables.
static member fucntions: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcceln…
static data members: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcceln…
A static variable within a function in C or C++ is like a global variable but just for that function.
A static variable within a class in C++ is a global variable, shared among all instances of that class. A static function within a class is the same as a standard C function as it exists on the global scope, it cannot access any non-static variables in the function though. A static C function is only seen within the file that it is defined.
A static method or variable in Java means that there is only one for that class, no matter how many objects of that type exist. It also means that you do not need to create an object of that type to call that method or access that variable.
That's why the main method in Java is static becuase you only want it executed once, and you shouldn't need to create an object of that type beforehand.
In a phone interview for Westwood studios, I was once asked what a static function was. My answer was. "I really have no idea, I could tell you what a static variable does." The guy laughed as said it was fine. Sure enough it was as I got the job :) Back then C was the main language we were coding in so he wanted the answer to be what Daemin said about it only being seen within the file it was defined. Now a-days though, working in C++ land, Static functions are your friends when working in a truly OOP style.
So, just remember, when preparing for job interviews, it's good to know a few obscure terms :)
Some tricky things that have caught me out in past interviews:
Multiple inheritance and where it can be useful. At the time I couldn't think of anywhere I'd want to use it.
What is the biggest number we can store in an C++ integer? I asked on what platform but forgot to ask about sign.
Why are man holes and their covers round?
The technical definition of a static variable, is that it occupies a static location in memory. That is, no matter what the program does, the variable will always occupy the same chunk of memory for the run-time of the program. This is in contrast to stack variables (whose location changes depending on whats on the stack) and dynamic/heap variables (whose location depends on where the memory allocator put it). However, as has been said static has other meanings as well :)
Also, static variables should not be seen as being any sort of exact equivalents for globals. Static variables in functions are only available in the scope of that function (although you could return a pointer/reference to the variable), and within functions, static variables are only initialised the first time they are encountered.
Possibly the more important thing is for global-scope static variables. Global variables will be initialised in the order they are encountered in the file/s. Static variables make no such guarantee, so you should never use one static variable to initialise another static variable! There is, however, a guarantee that both static and global variables will be destroyed in reverse order to the order they were initialised.
This is my recollection anyway...possibly based on older C++ standards :)
CYer, Blitz
textbook def - by prefixing the variable declaration with static the compiler maintaines the variables value even after losing scope.
Example given:
long IncreasePeopleCount()
{
long NumberOfPeople = 0;
NumberOfPeople =+ 1;
return NumberOfPeople;
}
main()
{
long Num;
Num = IncreasePeopleCount();
Num = IncreasePeopleCount();
}
What happens is that count will always be 1 because NumberOfPeople is reset to 0 each time the function is called. So two ways around that is creating a global variable or make it static (static long NumberOfPeople = 0 ).
That may not be the best explanation - so if one of the other programmers has more to add.....