In Java, static members (variables and methods) belong to the class rather than to any specific instance of the class. This means that static members can be accessed directly by the class name without creating an object of that class.
Static members are shared among all instances of a class. These members are initialized when the class is loaded into memory, and they are accessible using the class name.
Static members are used when we want to share data among all instances of a class, or if we want to define utility methods that don't rely on object instances.
A static variable is common to all instances of a class. When one object modifies the static variable, the change is reflected in all other instances.
class Counter { static int count = 0; Counter() { count++; // Incrementing static variable } static void displayCount() { System.out.println("Count: " + count); } } public class Main { public static void main(String[] args) { Counter c1 = new Counter(); Counter c2 = new Counter(); Counter.displayCount(); // Output will be 2 as it's shared across all instances } }
In the example above, the static variable count
is shared across all instances of the Counter
class. Each time a new object is created, the static variable is incremented, and the value is shared among all instances.
A static method can be called without creating an object of the class. Static methods can only access static variables and other static methods.
class MathUtils { static int add(int a, int b) { return a + b; } } public class Main { public static void main(String[] args) { int result = MathUtils.add(5, 10); // Calling static method without object System.out.println("Sum: " + result); } }
In this example, the add()
method is static, so it can be called directly using the class name MathUtils
, without creating an object.
A static block is used for static initializations of a class. It is executed once, when the class is loaded into memory, before any objects are created.
class StaticBlockExample { static { System.out.println("Static block is executed when class is loaded."); } public static void main(String[] args) { System.out.println("Main method executed."); } }
In this example, the static block will execute before the main method, demonstrating that static blocks are executed once when the class is loaded.
Static members are accessed using the class name, as they are associated with the class itself rather than with any specific instance.
class Example { static int staticVar = 10; static void staticMethod() { System.out.println("Static method called"); } } public class Main { public static void main(String[] args) { // Accessing static variable and method directly with class name System.out.println("Static Variable: " + Example.staticVar); Example.staticMethod(); } }
In this example, we access the static variable and method of the Example
class without creating an object.
Static members in Java are associated with the class itself rather than with any instance of the class. They provide a way to store and manage data that is shared by all instances of a class. Static methods are particularly useful for utility functions that donβt require instance data, making the code more efficient and reusable.
Quick Tip:
Use static members when you need to share data across all instances of a class, or when you want to define utility methods that donβt depend on object states.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!