can some one please explain what is static variable, i tried reading some books, tried googling but i am not able to understand what are its uses.

a static variable retain their value even after the function to which it belongs is executed.

i am not understanding what is meant by that.:confused:

Let's say we have the function shown below:

void func1() 
{
    int count = 0;
    // some other statements
    count++;
    cout << "current value of count: " << count << endl;
}

if you called the above function five times the value of count will always be 1. Why? Because the variable count gets created and intialized each and every time the func1 gets called. Now if you change func1 and made count variable static

void func1()
{
    static int count = 0;
    // some other statements
    count++;
    cout << "current value of count: " << count << endl;
}

you will get completely different results. If you call the func1() fives times now you will see is print out the numbers 1, 2, 3, 4, 5? The static creates the variable space but the count variable does not go away when the function terminates.

This is used when you want one or more values to still exist after the function terminates.

Edited 4 Years Ago by lafigueroa: n/a

so lets say every time a specific method or function func1 in the above code is called the value gets initialized from 0 again but if static is declared it stores the value and next time its called it gives 2 and so on right ?

also can you please explain this

int StaticExample :: staticVar=0;
//StaticExample is class name, staticVar is variable name
viod main()
{
staticExample ::display(); //display is method name here
}

without declaring an object how is it possible to call a function and how do we use these things in programming applications thx.

Ah, there it gets complicated, as static has a different meaning when applied to variables and methods in a class (as opposed to local variables in a function or method). Here, it indicates that the variable or method belong to the class, as opposed to the individual objects of that class. So, if you have a method declared static , as in this case, you would access it using the class name and the scope operator, rather than through an object variable's name. Static methods, in turn, cannot access instance variables, only class (static) variables, which again belong to the class as a whole and are, in effect, shared variables for all the members of the class.

BTW, don't ever use void main() ; while some compilers do allow it, the C++ standard specifically requires main() to be of type int .

Ah, there it gets complicated, as static has a different meaning when applied to variables and methods in a class (as opposed to local variables in a function or method). Here, it indicates that the variable or method belong to the class, as opposed to the individual objects of that class. So, if you have a method declared static , as in this case, you would access it using the class name and the scope operator, rather than through an object variable's name. Static methods, in turn, cannot access instance variables, only class (static) variables, which again belong to the class as a whole and are, in effect, shared variables for all the members of the class

sorry im not getting it, can you please elaborate with example code. :confused:

OK, let's see what we can do, using what is probably the most over-used example for this. Imagine you have a class - it doesn't matter much what the class is for - that needs to keep track of how many objects there are in the class, and to allow the program to access the object count. An easy way to do this is to have a static integer counter and a static accessor for it. In the class declaration you would have:

class MyClass
{
private:
    static int counter;
    // other instance and class variables go here

public:
    MyClass();
    ~MyClass();

    static int getCount()
    {
        return counter;
    };
    // other methods go here
};

int MyClass::counter = 0;

Then in the c'tor and d'tor you'd have:

MyClass::MyClass()
{
    counter++;
    // the rest of the initialization goes here    

}

MyClass::~MyClass()
{
    counter--;
}

Finally, in your actual program you'd create some objects of the class and get the number of them that you created:

int main()
{
    MyClass* myObjectArray = new MyClass[SOME_NUMBER];

    std::cout << "The number of objects created is " << MyClass::getCount() << std::endl;

    delete[] myObjectArray;

    std::cout << "The number of objects remaining is " << MyClass::getCount();
}

The output should be the number objects created (equal to SOME_NUMBER in this example), and then the number of objects after deleting the array (which should be zero).

Edited 4 Years Ago by Schol-R-LEA: n/a

This article has been dead for over six months. Start a new discussion instead.