Static methods belong to the class rather than to any specific instance of the class. This means that they can be called without creating an object of the class. Static methods are associated with the class itself and are often used to create utility methods or to access/manipulate static variables.
ClassName.staticMethod();
this
keyword because this
refers to the current object, and static methods are not called on objects.Math
class), or string manipulations (e.g., String.valueOf()
).To declare a static method, you use the static
keyword:
class MyClass {
// Static method
static void myStaticMethod() {
System.out.println("This is a static method.");
}
}
class MathUtils {
// Static method to calculate the square of a number
public static int square(int number) {
return number * number;
}
}
public class Main {
public static void main(String[] args) {
// Calling the static method using the class name
int result = MathUtils.square(5);
System.out.println("Square of 5 is: " + result);
}
}
Square of 5 is: 25
In this example:
square
method is declared as static
, so it can be called without creating an instance of MathUtils
.MathUtils.square(5)
.Static methods are often used in conjunction with static variables. Static variables, like static methods, are shared among all instances of a class.
class Counter {
static int count = 0; // Static variable
// Static method to increment the count
public static void increment() {
count++;
}
}
public class Main {
public static void main(String[] args) {
// Call static method to increment the static variable
Counter.increment();
System.out.println("Count after increment: " + Counter.count);
}
}
Count after increment: 1
In this example:
count
variable is static, meaning it belongs to the class and is shared across all instances.increment
method is static, so it can modify the static variable directly without needing any instances of the class.A common practice in Java is to use static methods in utility classes. These are classes that are not meant to be instantiated but instead provide helper methods for various operations. A good example is the Math
class, which contains methods like Math.sqrt()
and Math.pow()
.
public class MathHelper {
public static int add(int a, int b) {
return a + b;
}
public static int subtract(int a, int b) {
return a - b;
}
}
To use this class, you would call its methods like so:
int sum = MathHelper.add(10, 5);
int difference = MathHelper.subtract(10, 5);
In addition to static methods, Java also supports static blocks. Static blocks are used to initialize static variables or execute code when the class is loaded into memory, before any static methods or constructors are invoked.
class StaticBlockExample {
static int value;
// Static block
static {
value = 42;
System.out.println("Static block executed.");
}
// Static method
static void displayValue() {
System.out.println("Value is: " + value);
}
}
public class Main {
public static void main(String[] args) {
StaticBlockExample.displayValue();
}
}
Static block executed.
Value is: 42
In this example:
value
variable.displayValue
static method prints the value.this
reference is required).