First, lets speak about the scope of a variable. The scope of a variable is the section of the application that can see and manipulate that variable. If the variable is declared in a procedure when that procedure is called, only that procedure can use that variable. Hence that variable is considered "Local".
Now Static. Static variables are variables that are created with the static key word as in:- function ab(var1 as integer)as integer static iTotal as integer itotal =iTotal + var1 (rest of ccode goes here) end function
Now when this function is called the first time, iTotal will be assigned the value of var1. The next time the function is called, iTotal will retain the previous value and the new value of var1 will be added to it. Hope this helps. (theTeacha)
Static variables life time is not under any scope.It initializes only one time from creation to delete.let me explain by a pgm.
void main()
{
int a=1;
static s=1;
int k=0;
for(k=0;k<1;k++)
{
i++;
s++;
main();
}
printf("%d is i",&i);
printf(%d is S",&s);
getch();
}
Output will be
2 is i
3 is s
It Shows that local variable initialize every time the function call.But Static variable initializes only once
<><><>
For a variable, static means it keeps its value even when the enclosing code is not active--out of scope. A static variable is assigned to a fixed memory location when the program runs, so it is possible to examine a static variable at any time the program is active, even when the function where the var is declared is not active. A local variable is dynamically allocated storage space when a function is called, and its storage is given up when the function ends, so it only exists while the function is executing.
For a function, static declaration means it can only be called from functions in the same file (in C).
From Java point of view, static global class members are shared among all class instances and can be accessed from any class member [Methods]. While local global [attributes] can be accessed from any class member [method] but not shared with other class instances The global variable can be shared across multiple files. But the global static variable cannot be shared across multiple files. Hence if you want the variable should be modified in some other file, you can make it a global variable and if you want a global variable only in the file you are declaring and not to shared across, you make it static global. the following code snippet should be useful. //ext2.c int a=10; static int s_b=89; //ext.c extern int a; extern int s_b; int main() { printf("a = %d\n",a); printf("static b = %d\n",s_b); return 0; } then build the exe with ext.o and ext2.o and try running.. you will encounter an error like this "./ext.o(.text+0x20): undefined reference to `s_b'" Regards, ~Naren
Static global variable has scope only in the file in which it is declared. it can't be accessed in any other file but its value remains intact if code is running in some other file means lifetime is in complete programme while static local variable has scope in that function in which it is declared means it can't be used in other functions in the same file also, means scope is limited to the function in which it is declared while its life time is also through out the programme.
A local variable is a variable that is declared inside a code block. A code block typically starts with an opening brace and ends with a closing brace:
{
// code block here
}
The local variable is scoped to that block in which it is declared and will fall from scope when that code block ends.
A global variable is a variable that is not defined inside a code block. A global variable is scoped to the file or translation unit in which it is declared. Although we tend to think of a global as being globally accessible to all code, this is not the case at all. It is only globally accessible to code within the same translation unit.
If we want other translation units to be able access a global variable, then those translation units must redeclare the global with external linkage. This is never a good idea. Code that operates upon a variable should remain as closely-coupled to that variable as possible. This makes code easier to both read and maintain. If external code gains access to a variable, it becomes that much harder to determine when, where and why a variable's value is changing. Since there's no physical way to prevent an external translation unit from declaring external linkage to a global variable, the only solution is to avoid using global variables in the first place.
Object oriented programming provides the best solution by allowing the programmer to encapsulate the variable and the functions that specifically operate upon it as a single entity; a class. The variable should be declared a private static member of the class and the operations as static methods (public or private, depending on whether the methods should be accessible from outside of the class or not). Where external linkage is required, the class can grant friend access to the code that specifically requires it, rather than allowing the code to grant itself external linkage as it can with a global variable.
Global constants are less of a problem since a constant cannot change value. Thus there's no reason to prevent external-linkage to a constant. But with global variables, there's rarely a good reason to allow external-linkage. If a variable is truly global, then there's no reason not to use one, but there are actually very few good reasons where a truly global variable needs to exist. A trace log is a good example, as are standard input and output devices.
A static variable has a value that persists throughout the entire execution of the program. A global variable is declared at file scope, i.e. outside of any functions, and therefore has visibility (scope) from all functions, including functions in other compilation units that are linked together. A global variable also has value that persists throughout the entire execution of the program.
Do not confuse this with static global, which means that visibility is limited to function in the same compilation unit.
When you say static variable, do you mean local static variable or global static variable?
In C, the difference between global static variables and global variables is that static in this case means that the variable can be used only in the module (.c file) that it is declared.
The difference between a local static variable and a global variable is the scope: the local variable can be used only inside the function that declares it. So you can have 2 local static variables in the same file with the same name but in different functions and they will be 2 different variables.
difference between constant and static variables in java
'global static'?! There is no such thing.
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.
Global Varible: The variable which is declared as "Global" one : having the preveleges to use and access that variable in any class and object( means any where in the program) just like PUBLIC keyword in OOPS concepts. Static Variable : If we declare a variable as Static , then it wont have the permission to access that variable through out the program and u have to use it inside the class or object which u declared itself. All the Best Annapurna
Constants, static variables and global variables are allocated in the program's data segment at compile time. Local variables are allocated on the stack at runtime. Variables cannot be allocated on the heap, you must use a constant, static variable, global variable or local variable to store the start address of a dynamic memory allocation. The variable must be a raw pointer or a reference handle (a smart pointer).
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.
difference between constant and static variables in java
'global static'?! There is no such thing.
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.
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.
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
global and static
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
There are two ways to declare varibles. 1. Locally 2. Globally When you declare a variable locally in any function that means it is only accessible by that function. When you declare a variable globally so it is accessible by all the functions in the program. Declaring variables with static keyword means you are setting its value null.
Global Varible: The variable which is declared as "Global" one : having the preveleges to use and access that variable in any class and object( means any where in the program) just like PUBLIC keyword in OOPS concepts. Static Variable : If we declare a variable as Static , then it wont have the permission to access that variable through out the program and u have to use it inside the class or object which u declared itself. All the Best Annapurna
static: we can use the keyword static either to method or to a variable. when we declare to a method,(eg: public static void main(String args[]),we can use this method without any object. when we use to a variable,there will be only one instance of that variable irrespective of how many objects that get created of that class. Final: Usage of final to method or to a variable makes them as constant. It's value cannot be changed...
No. A static member variable is local to the class in which it is declared (much like a global, but scoped to the class) and is accessible to all instances of the class. Since it does not belong to any one instance of the class, it cannot be accessed via the this pointer, as you can with non-static members. Implicitly accessing the variable is the same as explicitly accessing it via ::.Note that it is possible to access a static member variable from outside the class by providing an accessor (getter) for it within the class. The accessor should be static as well, but needn't be, but it should return by value, otherwise it is no better than a global.