- Explore [has_child]
- All Courses [subitem]
- AI Career Platform [subitem]
- Hire form us [subitem]
- 1:1 Coaching/Mentoring [subitem]
- Job Board [subitem]
- Institute Partnerships [subitem]
- Resources [has_child]
- Master Classes [subitem]
- Discussion Forum [subitem]
- Coding Playground [subitem]
- Free Courses [subitem]
- Topics [has_child]
- Data Science [subitem]
- Software Development [subitem]
- Company Insights
- Interview Preparation [subitem]
- Python [subitem]
- Programming [subitem]
- Digital Marketing [subitem]
- Web Development [subitem]
- Success Stories [subitem]
Data Structures: Foundation of Effective Programming
A Career in Digital Marketing - A Complete Guide for Beginners
Advanced Algorithms and Problem Solving Techniques
Fundamentals of LCM and HCF-2
Java Programs: Know the Best Java Programs for Beginners
Python Libraries
Introduction to Natural Language Processing (NLP)
React Functional Components: Introduction
Does a Linkedin profile really matter before getting a Job?
Secrets Of A Good Resume, Which Will Get You Hired!
What Is Content Marketing?
What is the Scope of Digital Marketing in 2025
What is Email Marketing in Digital Marketing?
What is Consulting? Essential Insights for Aspiring Consultants
How To Start Your Career In Data Science
Basic Guide to HTML & CSS – The Fundamentals of Web Development
The Multifaceted Relationships Between Banker and Customer
Getting Started with Tableau: Installation and Introduction
Step-by-Step Guide to Data Visualization with Power BI
Discover the Versatility of Microsoft Excel: Your Swiss Army Knife for Data
Data Science vs. Data Analytics - What's the Difference?
Why is it Important for Freshers to Work in a Team?
Basics of Javascript
Operating System: Functions
OOPs (Object Oriented Programming) in C++
What is Grooming & Etiquette?
Introduction To Cloud Computing
What are Node.js and Basics of Node.js?
Why Learn English?
Header in C++
How to Apply for Jobs as Fresher & Get Selected in One Go
Why Social Media Marketing Is Important?
C++ Language: An Overview
The skills required to stay relevant in IT sector
Ultimate Guide to HR Interview Questions for Freshers
Software: Types & Definition
Job Trends In This Decade
Understand Serialization and Deserialization in Java
6 Bootstrap Tools and Playground – One-stop shop for all Web Developmental Needs
JSON vs XML: Differences
Learn about Boolean in Python
A-Z about Python Variables
Introduction to Big Data Analytics: From Basics to Implementation
All about C Programming Language
A quick guide to Asymptotic Analysis
Fundamentals of Divisibility Rules in Quantitative Aptitude
Data Communication: A Process
What are Collections in Python?
Introduction to Deep Learning: From Basics to Advanced Concepts
Introduction to Management Interview Preparation
How To Start Competitive Programming - A Complete Guide
Structure of DBMS
Introduction to Goal Setting and Risk Profiling
10 Common Data Structures Every Programmer Must Know
Introduction To SQL: A Complete Guide
Fundamentals of Digital Marketing
Javascript vs Typescript: What is the Difference?
What is Search Engine Optimization & How It Works
What is Full Stack Development?
Fundas of Pandas
Software Testing: What it is?
What is a Job Interview?
What is Digital Marketing & How to Become a Digital Marketer
Most Common 10 Telephonic Interview Questions
Mastering Vocabulary: The Key to Verbal Ability
Types of Data in Statistics
The Role and Importance of Banks in the Economy
What is Meant by Machine Learning & What Can Machine Learning Do?
The Ultimate Guide to Resume building for Freshers
All About Resume and Its Importance
Encapsulation in Java: A Comprehensive Tutorial
Why Group Discussion for Interview? The HR Perspective
Why Data Visualization is Important for Becoming a Data Scientist
Introduction to Big Data
Master Simple and Compound Interest Quickly and Accurately
Understanding the Loan Underwriting Process
The Essential SUM Function in Excel: A Step-by-Step Guide
Creating and Using Sets in Tableau: A Comprehensive Guide
Mastering SWOT Analysis for Business Success
System Testing: Explained
Understanding the Difference in Simple and Compound Interest over 2 and 3 Years
Understanding Risk and Return in Mutual Funds
Mastering Data Filters in Tableau: A Step-by-Step Guide
Mastering the MIN Function in Excel: Find the Lowest Value Instantly
Mastering Value Chain Analysis: Uncovering Business Opportunities
Understanding Principle Multiplication with Compound and Simple Interest
Creating Calculated Fields in Tableau: A Step-by-Step Guide
Understanding PESTEL Analysis for Industry Evaluation
Understanding Credit Scoring Models: A Comprehensive Insight
Understanding Simple Interest vs. Compound Interest
Combining Data Sources Using Data Blending in Tableau
Mastering the GE-McKinsey 9-Box Matrix: A Strategic Guide
Mastering the COUNT Function in Excel
Mastering Mutual Fund Investment and Redemption Plans
Mastering Mixtures and Alligations: Key Techniques and Practice
Mastering the COUNTA Function in Excel
Maximizing Organizational Performance with the Balanced Scorecard
Bringing in More Data with Joins in Tableau
Understanding Credit Rating Agencies and Their Processes
Mastering Bar Charts in Tableau: A Step-by-Step Guide
We will understand static variables along with its applications and use cases in C++ using examples in this article.
What is a Static Variable?
A var becomes a static variable if the static keyword was used before its definition.
Syntax
static <type> <variable_name>; |
The place where a static keyword is specified determines the purpose for which it is used. If a variable is defined within a class, all class instances—that is, all objects—will have the same version of the variables, and we may access it outside of the class without requiring any objects by calling it by its class name. Its value is maintained between calls if it is declared inside of a function for the duration of the programme, but it cannot be accessed outside of the function or scope in which it is defined. Let's look at some C++ examples of both categories of static variables.
Code
#include <iostream> |
Output: Num of 1st Object: 2 Num of 2nd Object: 2 |
Even though we haven't increased the num of the second object, the result shows that the values of num for both objects are the same. The fact that num has the same value in both objects shows that a static variable is shared by both of them.
Static Variable inside a Function
Code
#include <iostream> |
Output: 1 2 |
Watch the result in this scenario. In the main method, the increase() function has been called twice, with the second call returning the first call's increased value. This demonstrates that a static variable inside a function is only defined once during the initial function call, and all subsequent calls to the same function utilize the same copy.
All of the objects in a class share a static variable with it. A static variable included within a function or scope stays in memory for the duration of the programme.
Static Variable Uses
When we wish to reuse a variable's altered value from one function call to the next, we should utilize a static variable. Or if we want the class variable to remain constant across all objects. In this course, we learnt what a static variable in C++ is, how important it is, and when to utilize one.