Static Members in Java
🔷 Static Members in Java
✅ What is static in Java?
The static keyword in Java is used for memory management. It makes a member (variable or method) belong to the class rather than an instance of the class.
📌 Types of Static Members:
-
staticvariables (class variables) -
staticmethods -
staticblocks -
staticnested classes
✅ 1.1 Static Variable
-
Shared among all objects of a class.
-
Stored in method area (not heap).
-
Initialized only once at class loading time.
🔹 Example:
✅ Output:
✅ 1.2 Static Method
-
Can be called without creating an object.
-
Can only access static data directly.
-
Cannot use
thisorsuper.
🔹 Example:
✅ 1.3 Static Block
-
Used to initialize static variables.
-
Runs once when the class is loaded.
🔹 Example:
Comments
Post a Comment