C++ Static Data Members

Last Updated : 17 Jan, 2026

Static data members are class members declared using the static keyword. Unlike non-static members, a static data member belongs to the class itself, not to individual objects.

Characteristics of Static Data Members:

  • Only one copy of a static data member exists for the entire class, shared by all objects.
  • It is initialized once and exists independently of class objects.
  • Its lifetime spans the entire program execution.
  • Access control (public, private, protected) applies just like other class members.
  • It must be defined outside the class, except in specific cases (explained below).

Syntax

class ClassName {
static data_type data_member_name;
};

Static data members are useful for maintaining data shared among all instances of a class. The C++ Course explains how to implement static data members, ensuring you understand their significance in C++ programming.

Example: Below is the C++ program to demonstrate the working of static data members:

C++
// C++ Program to demonstrate static data members
#include <iostream>
using namespace std;

// class definition
class A {
public:
    static int x;  // static data member
    A() {
        cout << "A's constructor called" << endl;
    }
};

// definition and initialization of static data member
int A::x = 2;

int main() {
    // accessing static data member using scope resolution operator
    cout << "Accessing static data member: " << A::x << endl;
    return 0;
}

Output
Accessing static data member: 2

Defining Static Data Member

Static data members are declared inside the class but must be defined outside the class. If a static data member is not explicitly defined, the compiler will generate an error.

1. Declaration inside the class

class GFG {
public:
static int count; // Declaration only
};

2. Definition outside the class

int GFG::count = 0; // Definition and Initialization (outside the class)

Note: The static data members are initialized at compile time so the definition of static members should be present before the compilation of the program

Inline Definition of Static Data Members (C++17 and Later)

The C++ 17 introduced the inline definition of the static data members of type integral or enumeration which was not allowed in the previous standards. This simplifies the definition of the static data members.

Syntax

class A {
public:
static inline int x = 10;
};

This eliminates the need for a separate definition outside the class.

Accessing a Static Member

We can access the static data member without creating the instance of the class. Just remember that we need to initialize it beforehand. There are 2 ways of accessing static data members:

1. Using Class Name and Scope Resolution Operator

The class name and the scope resolution operator can be used to access the static data member even when there are no instances/objects of the class present in the scope.

Syntax

Class_Name :: var_name

Example:

A::x

2. Using an Object of the Class

We can also access the static data member using the objects of the class using dot operator.

Syntax

object_name . var_name

Example

obj.x

Note: The access to the static data member can be controlled by the class access modifiers.

Example to Verify Properties of Static Data Members

The following example demonstrates:

  • Static member initialization before object creation
  • A single shared instance across all objects
C++
#include <iostream>
using namespace std;

// helper class
class stMember {
public:
    int val;
    stMember(int v = 10) : val(v) {
        cout << "Static Object Created" << endl;
    }
};

// class with static data member
class A {
public:
    static stMember s;
    A() {
        cout << "A's Constructor Called" << endl;
    }
};

// static member definition
stMember A::s = stMember(11);

int main() {

    // accessing static member without creating an object
    cout << "accessing static member without creating the object: ";
    cout << A::s.val << endl << endl;

    // creating first object
    A obj1;
    cout << endl;

    // creating second object
    A obj2;
    cout << "Printing values from each object and classname" << endl;

    cout << "obj1.s.val: " << obj1.s.val << endl;
    cout << "obj2.s.val: " << obj2.s.val << endl;
    cout << "A::s.val: " << A::s.val << endl;

    return 0;
}

Output

Static Object Created
accessing static member without creating the object: 11

A's Constructor Called

A's Constructor Called
Printing values from each object and classname
obj1.s.val: 11
obj2.s.val: 11
A::s.val: 11

Note: In C++, we cannot declare static data members in local classes.

Comment