All Exams Test series for 1 year @ ₹349 only
Question

In object-oriented programming, which keyword ensures that a member variable belongs to the class itself rather than to any specific object, thereby allowing all instances to share a single copy of that variable?

The correct answer is

static

This question focuses on identifying the correct keyword in object-oriented programming that defines a member variable belonging to the class itself, rather than to individual objects.

The Role of Static Keywords in Class Variables

In object-oriented programming, when you define a class, it acts as a template for creating objects. Typically, each object created from this class (an instance) has its own separate copy of the member variables defined in the class. However, there are situations where you want a variable to be associated directly with the class itself, meaning there's only one copy of the variable, and all instances of the class share it.

The keyword used to achieve this is static. A static member variable has the following properties:

  • It belongs to the class, not to any specific object instance.
  • There is only one copy of the static variable, shared among all instances of the class.
  • It is initialized only once, typically when the class is loaded.
  • It can be accessed using the class name directly (e.g., ClassName.variableName) without needing to create an object instance.

This makes static variables ideal for constants that apply to the entire class or for variables that track class-wide information, such as a counter for the number of objects created.

Why Other Keywords Don't Fit

Let's consider the other options provided:

  • extern: This keyword is commonly used in C/C++ to declare that a variable or function is defined in another source file. It indicates linkage outside the current file but doesn't define class-level scope or sharing among object instances in the way static does.
  • volatile: This keyword tells the compiler that a variable's value might change unexpectedly from outside the normal program flow (e.g., by hardware or another thread). It's used to prevent compiler optimizations that might assume the variable's value remains unchanged. It does not relate to whether a variable belongs to a class or an object.
  • const: This keyword signifies that a variable's value cannot be changed after initialization, making it a constant. While a static variable can be declared as const (creating a class-level constant), the const keyword itself is about immutability, not about sharing a variable across all instances. The sharing mechanism is provided by static.

Summary of Keyword Functions

The static keyword is the specific tool for creating class-level variables that are shared across all instances.

Keyword Primary Function Scope Implication
static Variable/method belongs to the class itself. Shared among all instances; single copy.
extern Variable/function defined in another scope/file. External linkage.
volatile Variable value can change unexpectedly; prevents optimization. Related to memory visibility, not scope.
const Variable value cannot be changed after initialization. Immutability; can apply to instance or static variables.

Therefore, the correct keyword that ensures a member variable belongs to the class itself, allowing all instances to share a single copy, is static.

Was this answer helpful?

Important Questions from Programming in C

  1. Which one is the reserved word in C language ?

  2. Which of the following is the feature(s) of High level programming languages?

    I. Portability

    II. Easy debugging

  3. Which of the following is a type of subprogram?

    I. Function

    II. Subroutine

  4. The condition num! = 65 cannot be replaced by

  5. Which statement is false?

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App