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

C Program to Convert Decimal to Binary

In this tutorial, we will write a C program to convert decimal to binary. Before that, you must have knowledge of the following topics in …
Read More

C Program to Convert Binary to Decimal

In this tutorial, we will write a program to convert binary to decimal in C. Before that, you must have knowledge of the following topics …
Read More

C Program to Print ASCII Value of a Character

In this tutorial, we will write a program to print ASCII value of a character in C programming. Before that, you should have knowledge on …
Read More

Java Program to Convert Binary to Hexadecimal

In this tutorial, we will write a Java Program to Convert a Binary Number to Hexadecimal Number. Before that, you must have knowledge of the …
Read More

Java Program to Convert Hexadecimal to Binary

In this tutorial, we will write a Java Program to Convert a Hexadecimal Number to Binary Number. Before that, you must have knowledge of the …
Read More

Java Program to Convert Hexadecimal to Decimal

This is a Java Program to Convert a Hexadecimal Number to its decimal Equivalent. Before that, you must have knowledge of the following topics in …
Read More