How to Declare a Static Variable in a Class in C++? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 5 Likes Like Report 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 variable within a class we can use the static keyword in the definition while defining a static variable. Syntax to Declare Static Variable in C++To declare a static variable in a class use the below syntax: // inside classstatic dataType variableName = variableValue;C++ Program to Declare Static Variables in a ClassThe below example demonstrates how we can declare static variables in a class in C++. C++ // C++ program to declare static variable value #include <iostream> using namespace std; class myClass { public: static int s_value; // declaring the static member variable }; int myClass::s_value = 1; // defining the static member variable int main() { // can directly access the static variable through class cout << "Static variable value: " << myClass::s_value << endl; return 0; } OutputStatic variable value: 1 Static variables belongs to the class so we do not need to create an object to access the value of the static variables. Create Quiz Comment A anjalijhqgt7 Follow 5 Improve A anjalijhqgt7 Follow 5 Improve Article Tags : C++ Programs C++ cpp-class C++-Static Keyword Static Keyword CPP-OOPs CPP Examples +3 More Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like