Property | Local Variables | Instance Variables | Static Variables |
---|---|---|---|
Declaration | Declared inside methods, constructors, or blocks. | Declared inside a class but outside methods, constructors, or blocks. | Declared inside a class with the static keyword. |
Scope | Accessible only within the block, method, or constructor in which it is defined. | Accessible within the object of the class. | Accessible across all instances of the class using the class name. |
Memory Allocation | Memory is allocated when the block or method is executed and deallocated after execution. | Memory is allocated when the object is created and deallocated when the object is destroyed. | Memory is allocated at the class level and persists as long as the class is loaded in memory. |
Memory Area | Allocated in the stack memory area. | Allocated in the heap memory area. | Allocated in the method (or class) area. |
Default Values | Must be initialized before use; no default values are assigned. | Assigned default values if not explicitly initialized (e.g., 0 for int, null for objects). | Assigned default values if not explicitly initialized (e.g., 0 for int, null for objects). |
Access Modifiers | Cannot use access modifiers with local variables. | Can use access modifiers (e.g., private, public, protected). | Can use access modifiers (e.g., private, public, protected). |
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.