Jump to content

What are static variables?

Dobbsjr

Can somebody give me a simple explanation for static variables? If I google it, all that comes up is some vague statement about how static variables keep their value for the entire lifetime of a program and I have no idea what that means. 

Did my post help you? Then make sure to rate it!

Check out my post on symbolic links! || PSU ranking and tiers || Pokemon Thread

 

Link to comment
Share on other sites

Link to post
Share on other sites

Can somebody give me a simple explanation for static variables? If I google it, all that comes up is some vague statement about how static variables keep their value for the entire lifetime of a program and I have no idea what that means. 

For example, you created 3 objects of one class named SampleClass, which has a var named 'x'.

If x is static, it means that the value of x will be the same for all objects of this SampleClass.

Link to comment
Share on other sites

Link to post
Share on other sites

Can somebody give me a simple explanation for static variables? If I google it, all that comes up is some vague statement about how static variables keep their value for the entire lifetime of a program and I have no idea what that means. 

 

Here is a pretty good definition with links to more information, https://msdn.microsoft.com/en-us/library/aa691162(v=vs.71).aspx

Link to comment
Share on other sites

Link to post
Share on other sites

First of all what language are we talking about? In C++ static has 3 meanings. In C# there is one meaning.

C# meaning: In class, a variable that is shared between all objects. Unlike normal variables, which are unique to the object, a static variable is the same for all objects. 

For example:
class C

{

  public int foo = 0;

  public static int bar = 0;

  public void Baz()

  {

    foo += 1;

    bar += 1;

  }

}

 

//somewhere else

C c0 = new C();

C c1 = new C();

c0.Baz(); //foo == 1, bar == 1

c1.Baz(); //foo = 1, bar == 2. (bar is now also 2 for c0).

 

There are also static functions/methods. A static method doesn't require an object to be created to be called. So let's say we add a static method called 'Banana' to class C.

class C

{

  //...

  public static void Banana() { bar += 5; }

}

The static method can be called like this: C.Banana(). The static method can access all static variables, but not normal variables.

 

 


In C++ there are 3 contexts where you can use it.

 

The first one is in a class. This will work the exact same as in C#.

 

The second one is as a global variable. Here it means that the variable is private to that specific file (no linker visibility). This is so that when you have to files with and int called global, the linker won't be confused about 2 variables with the same name.

This also works for function. You can declare a function static, so that it is private to that file.

The third context is in a function.
Let's say I have this function

static void Foo()

{

  static int i = 0;

  i++;

  printf("%d\n", i);

}

 

There is quite a lot to this little bit of code. First of all the function is declared static, meaning it is private to this file. Then there is a static int i, i get's incremented and printed. What's gonna happen is the first time this function is ran, the variable i will be initialized to 0. The every time we call it, it will skip the initialization and use the last value.
So if I were to call the function 5 times, this would be the output:

Foo(); //1

Foo(); //2

Foo(); //3

Foo(); //4

Foo(); //5

 

I hope this answers your question. static is kind of a weird keyword in C and C++.

Link to comment
Share on other sites

Link to post
Share on other sites

Also one thing to note. If you create a static variable in a function, it will be initialized to zero by default, unlike a normal variable.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×