Static Keyword in Java
Introduction to Static Keyword in Java
The static keyword in Java is used to define class level members that belong to the class rather than individual objects. It plays an important role in memory management and is widely used in real world applications.
After understanding access modifiers, learning static helps in writing efficient and optimized Java programs.
What is Static Keyword in Java
The static keyword is used to declare variables, methods, and blocks that are shared among all objects of a class. These members are created only once and are stored in a common memory area.
Static members can be accessed without creating an object of the class.
Static Variable in Java
A static variable is shared among all instances of a class. It is initialized only once and stored in a common memory location.
Example of Static Variable
static int count = 0;
Counter() {
count++;
System.out.println(count);
}
}
public class Main {
public static void main(String[] args) {
new Counter();
new Counter();
new Counter();
}
}
In this example, the static variable count is shared by all objects and increases with each object creation.
Static Method in Java
A static method belongs to the class and can be called without creating an object. It can access only static variables and other static methods.
Example of Static Method
static int square(int num) {
return num * num;
}
}
public class Main {
public static void main(String[] args) {
System.out.println(MathUtils.square(5));
}
}
Static Block in Java
A static block is used to initialize static variables. It is executed only once when the class is loaded into memory.
Example of Static Block
static {
System.out.println(“Static block executed”);
}
}
Key Features of Static Keyword
Static members belong to the class, not objects. They are created once and shared across all instances. Static methods cannot access non static variables directly. Static blocks run only once during class loading.
Advantages of Static Keyword in Java
Static keyword reduces memory usage by sharing variables. It allows easy access to utility methods without creating objects. It improves performance and is useful for constants and common resources.
Disadvantages of Static Keyword
Overuse of static can reduce flexibility in code design. Static methods cannot access instance variables directly. It may lead to tightly coupled code if not used properly.
Real World Example of Static Keyword
In an application, a configuration class may store common settings like database connection details using static variables. Utility classes such as math functions also use static methods so they can be accessed without creating objects.
Common Mistakes in Static Keyword
Trying to access non static variables inside static methods directly causes errors. Overusing static variables can lead to poor design. Not understanding the difference between static and instance members is a common issue.
Interview Questions on Static Keyword in Java
What is static keyword in Java
What is a static variable
What is a static method
Can static methods access non static variables
What is a static block
FAQs
What is static keyword in Java in simple terms
Static keyword is used to create class level variables and methods shared by all objects.
Why do we use static in Java
It reduces memory usage and allows access without creating objects.
What is the difference between static and non static
Static members belong to the class while non static members belong to objects.
