Skip to main content

What means static?

Submitted by Kane on

Gday,

Just wondering what the static statement means in C++ and Java?

ie. public static void JavaMethod() or
static int variable;

Submitted by Jacana on Thu, 25/03/04 - 12:33 AMPermalink

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.....

Submitted by Kane on Thu, 25/03/04 - 12:49 AMPermalink

i think i understand...so even though the variable is assigned 0 at the start of the function, it will always be the last value it was set to?

actually, i think thats even more confusing that your example...[xx(]

Submitted by Jacana on Thu, 25/03/04 - 1:47 AMPermalink

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.

Submitted by Kane on Thu, 25/03/04 - 2:10 AMPermalink

ok then...thanks Jacana!

btw, r u going to the Indie Game Dev Conference?

Submitted by Jacana on Thu, 25/03/04 - 3:27 AMPermalink

Thanks for that Red :) I knew someone would have something more defenitive then I would (didn't think of MSDN )

Submitted by Jacana on Thu, 25/03/04 - 4:28 AMPermalink

Forgot to mention about static functions (as per Red's post) singletons would be used in that manner as you only want to create one instance (thus being singleton)

Submitted by Daemin on Thu, 25/03/04 - 8:22 AMPermalink

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.

Submitted by DaMunkee on Fri, 26/03/04 - 2:00 PMPermalink

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 :)

Submitted by Daemin on Fri, 26/03/04 - 11:26 PMPermalink

Yeah, and C or C++ is filled with them, well not really obscure, but just different meanings for the same word in different contexts. It's not really a beginner friendly language...

Submitted by Kane on Fri, 26/03/04 - 11:42 PMPermalink

C++ was the first language that I learned proprly and I thought it was fine...but then of course I had to move to Java and that was just a breeze...

Submitted by Barry Dahlberg on Sat, 27/03/04 - 1:25 AMPermalink

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?

Submitted by redwyre on Sat, 27/03/04 - 11:08 AMPermalink

One thing to note is that static non-member functions are depreciated, use un-named namespaces instead:

static void MyFunc() {}

should be:

namespace
{
void MyFunc() {}
}

Submitted by Blitz on Wed, 31/03/04 - 2:39 AMPermalink

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

Posted by Kane on

Gday,

Just wondering what the static statement means in C++ and Java?

ie. public static void JavaMethod() or
static int variable;


Submitted by Jacana on Thu, 25/03/04 - 12:33 AMPermalink

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.....

Submitted by Kane on Thu, 25/03/04 - 12:49 AMPermalink

i think i understand...so even though the variable is assigned 0 at the start of the function, it will always be the last value it was set to?

actually, i think thats even more confusing that your example...[xx(]

Submitted by Jacana on Thu, 25/03/04 - 1:47 AMPermalink

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.

Submitted by Kane on Thu, 25/03/04 - 2:10 AMPermalink

ok then...thanks Jacana!

btw, r u going to the Indie Game Dev Conference?

Submitted by Jacana on Thu, 25/03/04 - 3:27 AMPermalink

Thanks for that Red :) I knew someone would have something more defenitive then I would (didn't think of MSDN )

Submitted by Jacana on Thu, 25/03/04 - 4:28 AMPermalink

Forgot to mention about static functions (as per Red's post) singletons would be used in that manner as you only want to create one instance (thus being singleton)

Submitted by Daemin on Thu, 25/03/04 - 8:22 AMPermalink

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.

Submitted by DaMunkee on Fri, 26/03/04 - 2:00 PMPermalink

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 :)

Submitted by Daemin on Fri, 26/03/04 - 11:26 PMPermalink

Yeah, and C or C++ is filled with them, well not really obscure, but just different meanings for the same word in different contexts. It's not really a beginner friendly language...

Submitted by Kane on Fri, 26/03/04 - 11:42 PMPermalink

C++ was the first language that I learned proprly and I thought it was fine...but then of course I had to move to Java and that was just a breeze...

Submitted by Barry Dahlberg on Sat, 27/03/04 - 1:25 AMPermalink

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?

Submitted by redwyre on Sat, 27/03/04 - 11:08 AMPermalink

One thing to note is that static non-member functions are depreciated, use un-named namespaces instead:

static void MyFunc() {}

should be:

namespace
{
void MyFunc() {}
}

Submitted by Blitz on Wed, 31/03/04 - 2:39 AMPermalink

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