answersLogoWhite

0

If this is a homework assignment, you really should try to answer it on your own first, otherwise the value of the reinforcement of the lesson due to actually doing the assignment will be lost on you.

A static variable outside of a class is a variable whose value persists for the duration of the program's run time. Even if the variable goes out of scope, when it goes back into scope its value will be the last value that was assigned to it.

A static variable inside a class is a variable that is part of the entire class definition. There is only one copy of that variable, and it is shared by all instances of the class.

A static function is a method of a class that can only access static variables. It cannot access instance variables because it has no this pointer associated with its invocation. It is useful because you do not need an instance of the class to invoke it, so you can use it to iniitialize static class variables.

// using ellipses (...) to indicate tabs for clarity

class myclass {
... static c_Instances;
... static initClass () {
... ... c_Instances = 0;
... }
... myclass () { // constructor
... ... c_Instances++;
... ... // other stuff
... }
... ~myclass () { // destructor
... ... c_Instances--;
... ... // other stuff
... }
... // other stuff
}

// out of class invocation
myclass::initClass ();

User Avatar

Wiki User

15y ago

What else can I help you with?

Related Questions

Scope of static variables?

Scope of static variable is with in the file if it is static global. Scope of static variable is with in the function if variable is declared local to a function. But the life time is throughout the program


Why static variables in a function have global extent?

A variable declared static outside of a function has a scope the of the source file only. It is not a global variable. A variable declared outside of a function and without the static qualifier would be a global variable.


How do you create static variables in PHP?

Use the "static" keyword to declare a static variable within a function like shown below. <?php function fun() { static $variable; static $another_variable = ''; } ?>


What is the difference between a static variable a global variable and a local variable?

A static variable is a variable allocated in static storage. A local variable is a variable declared inside a function. A global variable is a variable declared outside of any class or function. Note that local variables and global variables can both be allocated in static storage.


How can you change the value of a constant variable in C?

You can change a static variable by putting the static variable into a function that has operations that you intend to execute upon the variable. Example, you want to change the static variable to another value or make an addition of 2. Put the source code inside a function with the static variable declared locally within the function.Every time you call the function, the static variable value will change. Take note that the static variable retains the last value you declared it in your function call.A more terse answerLocal variables declared as static are changed as normal; they are special in that their values persist across function calls.


Write the difference between global variable and static variable in context to function with suitable example?

Static VariableA variable that exists in only one location and is globally accessible by all instances of a class and also a variable for which memory remains allocated as long as the program executes.Global VariableA variable that can be accessed by all parts of a program so it does not belong to any subroutine in particular and can therefore can be accessed from any context in a program


What is a static variable within a PHP function?

A static variable is a variable that retains it's value over multiple calls to the function. When it is defined using the static keyword, that defining value is applied only on the first use. For example: function foo(){ static $bar = 5; $bar++; echo $bar . "\n"; } foo(); foo(); foo(); foo(); foo(); This code would output the following: 6 7 8 9 10


What is a good example of static?

int counter() { // This function returns how many times it has been called // Since this is a static variable, this line will only be called the first time the function is called static int count = 0; return(++count); }


Do static variable retain their values even after exiting the function?

Yes. That is the definition of static.


What is difference between Static and Global variable?

Static may be local of global -local static variable is limited to the function scope. and retain it's value when function is been called . compiler differentiate static variables with a prefix function name while dealing with same name static variable in different functions. - Global static variable is visible to all the function defined in the file and retain it value but it cannot be used outside this file. now Global Variable --- this variable is also visible to all of the functions inside the file but also can be used outside the file via extern keyword.


What is the definition of the term C static?

The term C static is a variable within computer programming in particular C Language. When set static the variable inside a function keeps its value between invocations.


How do you create an static int as an instance variable?

You create a static integer as an instance variable by declaring it in a class using a statement that consists of the privacy level, then the keywords "static" and "int", and finally, the name of the variable. For example:public class TheAnswerIsHere {public static int example = 0;}will define an int example with initial value 0. The variable is accessed through the statement TheAnswerIsHere.example.