static keyword in Java
π§ What is static
?
The static
keyword in Java is used to indicate that a field, method, or block belongs to the class rather than to any specific object. It means it can be accessed without creating an object.
πΈ Uses of static
:
-
Static Variables β Shared across all objects.
-
Static Methods β Can be called without an object.
-
Static Blocks β Run only once when the class is loaded.
π§ͺ Example:
π Calling Static Method:
π Key Points:
-
Saves memory because shared members are loaded once.
-
Useful for utility methods.
-
Static methods canβt access non-static data directly.