Advertisements
Advertisements
Question
Differentiate between local and member data.
Distinguish Between
Advertisements
Solution
| Feature | Local data | Member data |
| Declaration Location | Declared inside a method, constructor, or block. | Declared inside a class but outside any method. |
| Scope (Visibility) | Restricted only to the specific method where it is declared. | Accessible to all the methods within that class. |
| Lifetime | Created when the method starts and destroyed when the method exits | Created when an object is instantiated and destroyed with the object |
| Default Values | No default values. Must be manually initialised before use. | Automatically assigned default values (e.g., 0, 0.0, null, false). |
| Access Specifiers | Cannot use access modifiers like public, private, etc. | Can use access modifiers (public, private, protected, default). |
shaalaa.com
Is there an error in this question or solution?
Chapter 3: User-Defined Methods - Exercises [Page 91]
