- Irrespective of the number of objects created, only a single copy of the static member is created in memory.
- All objects of a class share the static member.
- All static data members are initiated to zero when the first object of that class is created.
- Static data members are visible only within the class but their lifetime is the entire program.
Static data members are usually used to maintain values that are common for the entire class. , For Example, to keep a track of how many objects of a particular class have been created.
Place of Storage:
Although static data members are declared inside a class, they are not considered to be a part of the objects. Consequently, their declaration in the class is not considered as their definition. A static data member is defined outside the class. This means that even though the static data member is declared in class scope, their definition persists in the entire file. A static member has file scope. However, since a static data member is declared inside the class, they can be accessed only by using the class name and the scope resolution operator.
Below is the program to illustrate memory allocation in static and non-static data members:
Program 1: to illustrate non-static members
1 1 1
All three objects of class GfG S1, S2, and S3 share the member functions but have a separate copy of the data member count. In main(), the set_count() is explicitly called to set the value of count to 1. Now, each object has the value of its count = 1.
Program 2: to illustrate static members:
3 3 3
All three objects of class GfG S1, S2, and S3 shares the member functions as well as the static data member. Only one copy of the static data member exists in the memory. There is no need for a function to explicitly set the value of count because the value of static data members has been initialized to 0 outside the class definition. Now, each object increments the value of count and hence the output.
Note: Memory for member functions and static data members is allocated per class and not per object. The class sample has no data member(except static count), but this does not mean no memory space will be allocated to the objects of Sample. In such cases, minimum memory is set aside for object. Therefore, the size of S1, S2, and S3 is 1 byte.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Static data members in C++
- What is Dynamic Memory Allocation?
- Implementation of all Partition Allocation Methods in Memory Management
- Initialization of data members
- Can we access private data members of a class without using a member or a friend function?
- Understanding storage of static methods and static variables in Java
- C++ Program to swap two members using Friend Function
- Introduction to memory and memory units
- First-Fit Allocation in Operating Systems
- Best-Fit Allocation in Operating System
- How to restrict dynamic allocation of objects in C++?
- Implementation of file allocation methods using vectors
- Static Keyword in C++
- C++ | Static Keyword | Question 4
- C++ | Static Keyword | Question 3
- C++ | Static Keyword | Question 2
- C++ | Static Keyword | Question 6
- C++ | Static Keyword | Question 1
- C++ | Static Keyword | Question 5
- When are static objects destroyed?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.