What is static data members in C++

Static data members are declared using the static keyword and also known as a class member. Even if you have many class objects, the class only has one copy of the static data member. This is because all objects share static data members. Static data members are initialized to zero whenever a first class object is created.

Syntax of the Static data members:
Programmer's Academy

The above syntax uses the static keyword. data_type is a C ++ data type like int, float, etc. data_member_name like data member.

For example:

Here, static data members are accessed through static member functions.

Programmer's Academy

Access static data members without static member functions:

Static data members can also be accessed through the class name without using static member functions (because they are members of the class). Here we need the scope resolution operator (SRO) :: to access static data members without static member functions.

Programmer's Academy
For example:
Programmer's Academy

In the above program example, ABC is a static data member and also a class members and can be accessed directly with the help of scope resolution operators.

Characteristics of Static data members:

Static data members have certain special characteristics. are: –

  • When the first object of the class is created, it is initialized to zero. No other initialization is allowed.
  • Only one copy of that member is made for the entire class and is shared by all objects of that class, regardless of the number of objects created.
  • Only visible within the classroom, but its lifespan is the entire program.

Conclusion of Static data members:

A static data member must declare global data to be updated while the program is in memory.

Non-member functions cannot access these members if the static data members are declared private. However, any member of the class can access the public static data members. Static data members must be created and initialized before the main function control block begins.


(Visited 54 times, 1 visits today)

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Ask ChatGPT
Set ChatGPT API key
Find your Secret API key in your ChatGPT User settings and paste it here to connect ChatGPT with your Tutor LMS website.
Would love your thoughts, please comment.x