C++ Static Keyword3 min read

Static is a keyword in C++ that is when used with different types gives different meanings. static keyword can be used with variables in a function or class or with the static members of a class such as Class objects and Functions in a class.

When is used with any of the mentioned above, it cannot be modified in the future. static elements are allocated storage only once in a program lifetime.

Static Keyword can be used with the following in c++.

  1. Static variables in a functions
  2. Static Class Objects
  3. Static member Variable in class
  4. Static Methods in a class

1. Static variables in a Function

A variable declared static in a program is allocated to a static storage area. The static variables are only initialized only once in a program even if it is called multiple times in a program. It is useful when you do need to reset the counter even after multiple calls of the function.

Example of a static variable inside a function in c++

Output:


2. Static class Objects

The class objects are declared as static works the same in the same way as static variables. The objects declared static are allocated to a static storage area and have a scope till the lifetime of the program.

Example of a static class object in c++

Output:

As you can see the destructor is called after the end of the main function because the scope of static object is throughout the lifetime of the program.


3. Static Data Members inside a class:

Static variables of a class are shared by all the objects and these members are initialized once and allocated a separate single static storage. Each object does not have a separate copy of those variables.

Also, static data members are not initialized using the constructor as they are not dependent on variable initialization.

Example of a Static Data Members inside a class in c++

Output:

As you can see, it has to be explicitly initialized outside the class with a class name and scope resolution operator.


4. Static function in a class

Just like static data variables in a class, the static function also does not depend on the object of the class. We cannot call or invoke the static function of a class using object or ‘.‘ operator. But can be invoked using the class name and the scope resolution operator.

However, these functions can only access the static data members and static member functions as it does not have ‘this‘ keyword.

Example of a Static function in a class in c++

Output:


MORE

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …