How to Declare a Static Data Member in a Class in C++? Last Updated : 22 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, when we declare a member as static, then it means that the member will have only one copy that is shared by all objects of that class and we don't need any object to access that member In this article, we will learn how to declare a static data member in a class in C++. Declaring Static Data Member in a Class in C++To declare a static member within a class we can use the static keyword in the definition while defining a static member inside a class. Syntax to Declare Static Member in C++//inside class static dataType dataMemberName;Static data members must be initialized outside the class definition. C++ Program to Declare a Static Member in a ClassThe below example demonstrates how we can declare and access the static members in a class in C++. C++ // C++ program to declare static member in a class #include <iostream> using namespace std; class MyClass { public: static int staticVar; // Declaring static member variable static void staticFunc() { // Declaring and defining static member function cout << "Value of static variable: " << staticVar << endl; } }; // Defining and initializing static member variable int MyClass::staticVar = 0; int main() { // Accessing static member variable without creating an // instance of MyClass MyClass::staticVar = 10; // Calling a static member function without creating an // instance of MyClass MyClass::staticFunc(); return 0; } OutputValue of static variable: 10 Note: Static members belongs to the class so we do not need to create an object to access the value of the static members. Comment More infoAdvertise with us Next Article How to Create a Class with Private and Public Members in C++? S sheikfat8u Follow Improve Article Tags : C++ Programs C++ cpp-class C++-Static Keyword CPP-OOPs CPP Examples +2 More Practice Tags : CPP Similar Reads How to Declare a Static Member Function in a Class in C++? In C++, static functions are functions that are directly associated with a class so we can access the static function directly without creating an object of the class using the scope resolution operator. In this article, we will learn how we can declare a static function in a class in C++. Declare a 1 min read How to Declare a Static Variable in a Class in C++? In C++, a static variable is initialized only once and exists independently of any class objects so they can be accessed without creating an instance of the class. In this article, we will learn how to declare a static variable in a class in C++. Static Variable in a Class in C++To declare a static 2 min read How to Create a Class with Private and Public Members in C++? In C++, the classes are blueprints for creating objects with specific properties and methods that provide a feature of access specifiers to the user through which they can control the access of the data members present in a class. In this article, we will learn how to create a class with private and 3 min read How to Create a Static Library in C++? C++ allows users to add their own libraries to reduce the amount of code we have to write in our program. A static library in C++ is a library that is linked to the program at compile-time. In this article, we will learn how to create a static library in C++ from scratch. Create Static Library in C+ 3 min read How to Create a Template Class in C++? In C++, template classes are used to create generic classes that can work for different data types. In this article, we will learn how to create a template class in C++. Create a Template Class in C++ To create a template class in C++, we can follow the below syntax: Syntax of Template Classtemplate 2 min read How to Define a Static Data Member of Type const std::string? In C++, static data members are members that exist independently of any object of the class. A static data member is shared by all objects of the class, meaning there is only one copy of the static member that is shared by all the objects of the class. In this article, we will learn how to define a 2 min read Like