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?
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.
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:
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.
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.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.
Which one is the reserved word in C language ?
Which of the following is the feature(s) of High level programming languages?
I. Portability
II. Easy debugging
Which of the following is a type of subprogram?
I. Function
II. Subroutine
The condition num! = 65 cannot be replaced by
Which statement is false?