๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Local vs Instance vs Static Variables in Java  


  • Below is the differences between local, instance, and static variables in Java:
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).