In this post, we will learn the difference between Static Block and Static Method in Java. This is a frequently asked question in Java interviews for beginners. Let's dive into it.
Feature | Static Block | Static Method |
---|---|---|
Definition | A static block is a block of code inside a class that is executed only once when the class is loaded into memory. | A static method is a method declared with the static keyword, and it belongs to the class rather than an instance of the class. |
Execution Time | The static block is executed when the class is loaded into memory before the class's constructor or any static method is called. | A static method can be called at any time, either before or after the class is loaded. |
Return Type | Static blocks do not have a return type because they are not methods. | Static methods have a return type, and they can return a value or void if they don't return anything. |
Usage | Static blocks are mainly used to perform one-time initialization tasks for the class, such as initializing static variables or setting up configurations. | Static methods are used for tasks that are associated with the class and don't depend on the state of any particular instance. They can be called directly using the class name. |
Syntax | Static blocks are defined using the static keyword followed by a block of code enclosed in curly braces {}. | Static methods are defined using the static keyword followed by the method's return type, name, and parameter list. |
Accessibility | Static blocks can access only static members (static variables and static methods) of the class. They cannot access instances, or members directly. | Static methods can access both static members and instance members (non-static) of the class. |
Overloading | Static blocks cannot be overloaded because they are not methods with specific signatures. | Static methods can be overloaded by defining multiple methods with the same name but different parameter lists. |
Inheritance | Static blocks are not inherited by subclasses. | Static methods can be inherited by subclasses, and they can be overridden in subclasses using @Override annotation. |
Let's create an example to demonstrate the difference between static blocks and static methods in Java.
Static Block:
A static block is used to initialize static variables or perform other one-time actions when the class is loaded into memory. The static block is executed only once, the first time the class is accessed or instantiated.
class MyClass {
static int number;
static {
System.out.println("Static block is executed.");
number = 10;
}
}
public class StaticBlockVsStaticMethod {
public static void main(String[] args) {
System.out.println("Accessing the class for the first time...");
MyClass obj = new MyClass();
System.out.println("Number: " + MyClass.number);
}
}
Output:
Accessing the class for the first time...
Static block is executed.
Number: 10
Static Method:
A static method is a method that belongs to the class and can be invoked using the class name, without creating an instance of the class.
class MathUtils {
public static int add(int a, int b) {
return a + b;
}
}
public class StaticBlockVsStaticMethod {
public static void main(String[] args) {
int result = MathUtils.add(5, 3);
System.out.println("Result: " + result);
}
}
Output:
Result: 8
Interview Questions
Java
X vs Y
Comments
Post a Comment