Q16: How does the static keyword operate on variables, methods, blocks, and nested classes?
- Static Variables: Shared across all instances of a class; stored in heap class metadata area.
- Static Methods: Bound to the class context. Cannot access instance fields (
this/super). - Static Blocks: Execute once when the class is loaded into the JVM by the ClassLoader.
- Static Nested Classes: Nested classes that do not hold an implicit reference to an outer class instance.
Key Takeaways
- Static members belong to the class, not individual instance allocations.
- Static blocks run during class loading before constructor invocations.
Knowledge Check
Can a static method access a non-static instance variable directly without instantiating an object?