The static keyword in Java is used to indicate that a particular field, method, or block belongs to the class rather than instances of the class (objects). This means that static members are shared among all instances of the class. They can be accessed without creating an object of the class.
use of static keyword
- Static Variables
- Static Methods
- Static Blocks
- Static Classes
Static Variables (Class Variables)
A static variable is shared by all instances of a class. It is initialized only once, and its value is the same for all instances of the class.
Characteristics of Static Variables:
- They belong to the class, not the instance.
- They are initialized only once when the class is loaded.
- A static variable is accessible by all instances of the class and can be modified by any of them.
- They are used when you want to have a value that is common to all instances of the class.
Example of Static Variable
class Employee {
// Static variable
static int count = 0;
String name;
Employee(String name) {
this.name = name;
count++; // Increment the static variable
}
static void displayCount() {
System.out.println("Total Employees: " + count);
}
}
public class Main {
public static void main(String[] args) {
Employee emp1 = new Employee("John");
Employee emp2 = new Employee("Tom");
// Accessing the static variable via the class name
Employee.displayCount(); // Output: Total Employees: 2
}
}
Output:
Explanation:
In this case, the count variable is static. No matter how many instances of Employee you create, the count variable will store the total number of Employee objects created. All objects share the same count.
Static Methods
A static method belongs to the class, not an instance of the class. Therefore, static methods can be called without creating an object of the class. Static methods can only directly access other static members (static variables and static methods). They cannot access instance variables or instance methods directly.
Characteristics of Static Methods:
- They can be called using the class name (e.g., ClassName.methodName()).
- They can’t access instance variables or instance methods.
- They can only access static variables and static methods.
Example:
class MathCalculation {
static int add(int a, int b) {
return a + b;
}
static int subtract(int a, int b) {
return a - b;
}
}
class Main {
public static void main(String[] args) {
// Calling static methods using the class name
int sum = MathCalculation.add(50, 20);
int diff = MathCalculation.subtract(50, 20);
System.out.println("Sum: " + sum); // Output: Sum: 70
System.out.println("Difference: " + diff); // Output: Difference: 30
}
}
Explanation:
You can call the static methods add and subtract without creating an instance of the MathCalculation class. Static methods are accessed through the class name, such as MathCalculation.add(50, 20).
Static Blocks
A static block is a block of code inside a class that is executed only once when the class is loaded into memory. Static blocks are often used to initialize static variables or perform one-time setup tasks.
Characteristics of Static Blocks
1. A class can have multiple static blocks.
2. They are executed when the class is first loaded into the JVM.
3. They are executed only once, even if the class is used to create multiple objects.
Example1:
class StacticBlockExample{
static {
System.out.println("Static block executed\n");
}
public static void main(String[] args) {
// Static block is executed when the class is loaded
System.out.println("Hello World!");
}
}
Output:
Hello World!
Note:- Static Block is executed before any method in the main method.
Static Classes (Nested Static Classes)
In Java, you can have static nested classes. A static nested class is a nested class that is declared static. Unlike non-static nested classes, a static nested class can be instantiated without a reference to an outer class object.
Characteristics of Static Nested Classes
1. They do not have access to the instance variables and methods of the outer class.
2. They can only access the static members (variables and methods) of the outer class.
3. They can be instantiated without creating an instance of the outer class.
Example:
class OuterClass {
static String greetings = "Hello Friends!";
// Static nested class
static class InnerClass {
void displayGreetings() {
System.out.println("Outer Static Variable: " + greetings);
}
}
}
class Main {
public static void main(String[] args) {
// Creating an object of the static nested class
OuterClass.InnerClass inner = new OuterClass.InnerClass();
inner.displayGreetings(); // Output: Hello World!
}
}
Explanation:
The static nested class InnerClass can be instantiated without an instance of OuterClass. It can access static members of the outer class, such as greetings.
Static Import
Java allows you to import static members (fields and methods) of a class. This makes it possible to access those static members without the class name.
import static java.lang.Math.*;
class Main {
public static void main(String[] args) {
// Using static imports to call methods without the class name
double result = sqrt(25); // Calls Math.sqrt(25) directly
System.out.println("Square root of 25: " + result);
}
}
Output:
Explanation:
In this example, the sqrt method from the Math class is imported statically, so you can use it directly without specifying Math.sqrt.