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:

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.

Practice Your Code Here

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.

For example:

Practice Your Code Here

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.

Recommended Articles:

  1. What is Copy Constructor in C++
  2. What is Data Abstraction in C++
  3. What is Encapsulation in C++
  4. What is Access Modifiers in C++
  5. Classes and Objects in C++


Help Me Please: If you found any error in the content or example please let us know by comment also write your suggestion or if any doubt in the concept please feel free to write us in the comment section.

Practice your Code online here

(Visited 96 times, 1 visits today)

0 0 votes
Article Rating
Notify of
1 Comment
Newest Most Voted
Inline Feedbacks
View all comments

[…] What is static data members in C++ […]

You cannot copy content of this page
Scroll Up
Would love your thoughts, please comment.x