What is Static Data Members in C++?

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

A static data member in C++ is a data member defined in a class that is not instantiated with each object created of the class. Data members defined in a class are usually instantiated with every object created of the class. That is, each object of the class will have their own instances or copies of the data members defined in the class. However, if a data member is initialized with the static keyword, that data member will not be instantiated and there shall exist only one copy of that data member for all objects or instances of the class.

A static data member in C++ can be defined by using the static keyword. Outside of classes, the static keyword is used to define elements of the static storage class. A static data member in C++ is declared inside a class but is defined within static member functions or outside the class using a scope resolution operator(::).

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Syntax

A static data member in C++ can be defined by using the static keyword. The static keyword appears before other specifiers like data type, etc. A static data member can be declared in the following way:

For example:

The example above declares a static data member x within class A. A non-const, that is, a static data member which is not declared as const in C++ cannot be defined or initialized within the class in which it is declared. It is instead defined outside the class using the scope resolution operator as in the following example:

Static Member Functions

A static member function is a member function of a class that is defined with the static keyword. For example:

The sayHello() function above is a static member function of class A.

  • Static data members of a class can only be defined within static member functions and a static member function can call other static member functions of the class.
  • A static member function cannot define a non-static data member and cannot call a non-static member function.
  • A static member function need not be instantiated and can be called without creating any object of the class by using the scope resolution operator(::).
Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Static Data Members

A static data member in C++ is a data member defined in a class that is not instantiated with each object created of the class. In C++, each object or instance of a class consists of its own set of data members that are defined within the class. However, data members that are specified with the static keyword are not instantiated for each object of the class and the class will have only one instance of that data member.

Static data members in C++ are not associated with any object and can be accessed even without the creation of any object. Static data members belong to the static storage class in C++ and as such have a lifetime equal to the lifetime of the program.

Static data members cannot be declared or manipulated by normal member functions and cannot be accessed directly through an object of the class. A static data member is defined with the help of the scope resolution operator and is defined outside the class. A static data member in C++ can only be manipulated with the help of a static member function as in the following example.

Output:

In the above example, the data member x of class A is declared as a static data member. It is defined outside the class with the help of the scope resolution operator(::) and is accessed and manipulated with the help of a static member function.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Constant Static Members

A non-const static data member in C++ cannot be initialized within the class in which it is declared. However, if a static data member of integral or enumeration type is declared const it can be initialized inside the class definition as in the following example:

For static data members of LiteralType which are declared using the constexpr keyword, they must be initialized with an initializer right inside the class definition as in the following example:

Accessing Static Data Member Without Static Member Function

As mentioned above, a static data member in C++ can be defined with the help of the scope resolution operator(::). Similarly, static data members can also be directly accessed with the help of the scope resolution operator(::) as in the following example:

Output:

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Examples

Example 1:

Output:

In the example above, the static member function getStatic() is used to access the static data member x of class A.

Example 2:

Output:

In the example above, we have used the scope resolution operator to access and increment the static member x of class A. It is important to note that in this example, there are no objects of class A created and yet we are able to manipulate the value of the static data member.

Example 3:

Output:

The above example is an important use case of static data member in C++. The constructor of the class A increments the value of the static data member x. Thus, we can use the static data member to keep count of the number of times an object of class A is created in the program.

Transform your passion for coding into C++ expertise. Join Scaler Topics certification course and become a proficient C++ developer.

Conclusion

  • Static data members in C++ are not instantiated for each object of a class and instead, only one copy of the data member exists for the entire class.
  • A static data member in C++ is declared within a class and is defined outside the class.
  • A static data member in C++ can be accessed with the help of the scope resolution operator(::) or a static member function.
  • A constant static data member in C++ can be initialized within the same class in which it is defined.