Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
2.53/5 (3 votes)
I've read many times the word static. somewhere it is a Static Class. Static Function and Static Variable even a static array. what is the actual concept of this word in programming?
Posted

Roughly speaking, in many OOP languages the static qualifier is used for class methods, that is methods you may call without having a instance of the class (technically they are similar to C functions, that is methods without the hidden this parameter).

However, if you really want to know exactly the different meanings (pay attention: different meanings even in the context of single programming language, consider, for instance, C++) of the static qualifier then you have to choose the programming language and study its reference manual.
 
Share this answer
 
According to the standard C++, static refers to the storage duration of an object. The storage duration is therefore a property of an object, and it defines the lifetime the contents of the storage should have. In C++ there are four storage durations:
• static storage duration
• thread storage duration
• automatic storage duration
• dynamic storage duration

A storage duration is specified when an object is constructed. For example

static const int cte_LicId = 123455;

As such the storage of this object should last the entire time the program runs. In general, if a variable does not have dynamic storage duration, does not have thread storage duration, and is
not a local variable then it has a static storage duration. The storage for such an entity will last for the duration of the program (i.e. a global variable has static storage duration even if it not explicitly stated in his declaration)

In the above example, the static storage duration is associated with the object, cte_LicId, introduced by the declaration. As such the storage of this object should last the entire time of the program's run. In general, if a variable does not have dynamic storage duration, does not have thread storage duration, and is not a local variable then it has a static storage duration. The storage for such an entity shall last for the duration of the program (i.e. a global variable has static storage duration even if it not explicitly specified)


A local variable can be declared with static storage duration using the keyword static.

int foo(int i) {
static int s = rand(125);
return i+1;
}

Similarly for a class data member. Please refer to your C++ reference guide for more info.
 
Share this answer
 
There is no common concept in all the languages. The most similarity is with variables.
The table below covers C, C++ and C# in a maybe a bit simplistic way. What you see is the various uses of static.

Languagestatic file level variablestatic function level variablestatic file level functionstatic classstatic member variablestatic member functionstatic constructor
Cinternal linkage, lives from the point of declaration until the end of the programno linkage, lives from the point of declaration until the end of the programinternal linkagen/an/an/an/a
C++same as Csame as Csame as Cn/ashared between all instances of the classno instance function, has no hidden *this* parametern/a
C#n/an/an/aclass with only static fields (member variables) and methods (member functions)same as C++same as C++called before the first element of the class is accessed from outside

Cheers
Andi
 
Share this answer
 
v2
Comments
adriancs 10-Sep-14 20:10pm    
Nice answer! vote 5
Andreas Gieriet 11-Sep-14 1:30am    
Thanks for your 5!
Cheers
Andi
See here[^]

Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object.



When a member of a class is static you can see it as a member that belongs to the "object template". A class can be seen as an object template, able to create many objects of a certain type all with the same properties (but different values for each property)

eg.

C#
class MyObjectTemplate{
  public static string MystaticMember = "Static value, belonging to the class, not the instance of a class";
  public string MyUsualMember = "Normal value, belonging to the instance of the class";
 
}

class TestStatic{
   public static void Main(){
      MyObjectTemplate obj1 = new MyObjectTemplate();
      MyObjectTemplate obj2 = new MyObjectTemplate();
      obj2.MyUsualMember = "new value for obj1";  //now only obj2's member value is changed, ob1's value is still the same.
      //obj1.MystaticMember -> can't
      MyObjectTemplate.MystaticMember = "change this";  //now it is changed for everyone.
   }
}


Only use static when it makes absolute sense. Since you cannot call object members from static members (unless you create one in the static method eg) you end up in creating everything static before you know it and that goes against Object Oriented principles.

try to find some examples and make some yourself. Correct usage can help you a lot, incorrect usage can drown your application into a useless pile of rubbish.

Hope this helps.
 
Share this answer
 
a static variable is a variable that has been allocated statically—whose lifetime or "extent" extends across the entire run of the program. This is in contrast to the more ephemeral automatic variables (local variables are generally automatic), whose storage is allocated and deallocated on the call stack; and in contrast to objects whose storage is dynamically allocated in heap memory.

When a program (executable or library) is loaded into memory, static variables are stored in the data segment of the program's address space (if initialized), or the BSS segment (if uninitialized), and are stored in corresponding sections of object files prior to loading.
 
Share this answer
 
Comments
Ammar Shaukat 10-Sep-14 8:17am    
hmmm understand a little.. but what about static functions , static classes etc
bharat Parsiya 10-Sep-14 12:59pm    
See when you have static variables. you can change or access their values in static functions only. and at times we need to change values of this variables so static function comes in picture
Andreas Gieriet 10-Sep-14 18:49pm    
Wrong for at least C/C++/C#, sorry. Static variables can be read and written from any function that "sees" that variable.
Andi
static means: direct access, always available, always accessible and changeable anywhere while the app is still alive (running).

and you can't really learn it in theory.
go ahead and do some real code on static.

Code once, then you'll know it better than text explained here.
 
Share this answer
 
v2
Comments
Ammar Shaukat 10-Sep-14 8:15am    
i've learned few porgramming languages.. but never understand its use in real time applications...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900