History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
Static keyword a.z
1. 1
StaticKeywordandmeaning
Static Keyword
Static as the wordimplies are variables which exist for a certain amount of time,
much longer than that by ordinary automaticvariables.
The Many Meanings of the “Static” Keyword in C++
Private Linkage
Function Call Spanning
Per-ClassData
The two odd meanings of static:
Static is incredibly odd in the sense that it has two very different--
almost opposite-- meanings. The first meaning applies when it is used
with functions, and the second when it is used with variables. Let's take
a closer look.
The first meaning static functions:
In order totest staticfunctions you first need tobe able to compile multiple
source files-- I assumeyou havethe ability toproduce twodifferent sourcefiles
and compile them. With that addressed-- let's continue!
Static functions are much like private methods in Javaor C++. A privatemethod
is a methodwhich is only used by a class andcan't be used outsideof it. In C, we
can declare a staticfunction. A staticfunction is a function which can only be
usedwithin the sourcefile it is declared in.
First, I will give you a working example. There are twofiles-- main.cwhich is our
2. 2
StaticKeywordandmeaning
main program. Ourmain program will call our custom mini-library called
randomnum. Wedesigned ourrandomnum library to consist of 2 functions. One
is a public function called getRandomNum which returns arandom number
between 1-1000. Thesecondfunction in the library is aprivate function called
seedRandom. Anyone that has usedrand in C knows you need toseed rand. In
this case wemake seed a private function sotheoutsidesource files can't mess
with it. This is an exampleof encapsulation-- wehide the details of our library.
1 #include <stdio.h>
2 #include "randomnum.h"
3
4 int main(void)
5 {
6 printf("Random Number - %dn", getRandomNum());
7 return 0;
8 }
The second meaning-- static variables:
The second meaning is when static is used with a variable. This has
nothing to do with static in the function sense. Static when used with a
variable means the storage will be allocated at compile time and
remain intact for the entire cycle of the program's execution. You may
be thinking, "Isn't a global variable the same thing?" and you are right.
A global variable uses static storage automatically. You probably hear
people saying, "You should avoid global variables at all cost!" and they
are also right. Global variables violate encapsulation because they
don't hide data-- they simply give it to everyone. That is generally a
3. 3
StaticKeywordandmeaning
bad style to program with.
I won't go into using global variables because it's something that
should be avoided. I will show a snippet of how you could use static
inside a function that returns a variable.
#include <stdio.h>
02
03 char* getBob(void)
04 {
05 static char bob[3] = "bob";
06 return bob;
07
}
08
09 int main(void)
10 {
11 char *name = getBob();
12 printf("%sn", name);
13 return 0;
14 }
Why declare bob as static? Because bob was declared inside a function.
A function has auto (automatic) storage. That means when the
function returns those variables are popped off the stack and never to
be seen again (can be over wrote by new variables popped on the
stack). If you used this code without static it may or may not display
the correct name in this simple case. It's possible the memory would
still be intact and display the name, but the behaviour is actually
4. 4
StaticKeywordandmeaning
undefined if you don't include static. Static makes sure the variable is
not automatic-- instead it is allocated for the remainder of the
program.
To conclude static variables-- for the most part they are not needed
(like in the global variable case). When declaring a global variable static
storage is automatically assigned and using the static keyword is pretty
much pointless. When using static in other contexts it may be useful if
you need to return data that must remain intact for the remainder of
the program. Think about it carefully before deciding to use it!
Static: The Multipurpose Keyword
Writing about the const keywordbrings meto asubtlebut important distinction
between const and readonly keywords in C#: const variables are implicitly static
and they need to be defined when declared. readonly variables are not implicitly
staticand can only be initialized once.
E.g.: You are writing a car racing program in which the racing track has a fixed
length of 100 Km. You can define a const variable todenote this as
private const int trackLength = 100;
Static class declarations
Also, the static keyword in C++ is used to specifythat variables
will be in memory till the time the program ends; and initialized
onlyonce. Just likeC# and Java, these variablesdon’t need an
object to be declaredto use them. Pleasesee this linkfor the
use of the static keyword in C++: