The static keyword in Java is one of the most important concepts in object-oriented programming. In Java, the static keyword indicates that a member belongs to the class rather than to any specific instance (object).
This means static members are shared across all instances of the class, instead of being unique to each object. You do not need to create an object to access static members; they can be accessed using the class name.
The static keyword is primarily used for memory management, as it allows the sharing of variables and methods across all objects of a class.

Why Use Static Keyword in Java?
We use the static keyword due to the following reasons:
- Static variables are shared by all objects of the class, so only one copy is created instead of multiple copies.
- You do not need to create an object of the class to access static variables and static methods; they can be accessed using the class name.
- The static keyword is useful when you want to share common data among all objects of the class.
- It is used to create utility methods (like
Math.sqrt()) that can be called directly without creating an object. - Static members are loaded once when the class is loaded into memory, which avoids repeated initialization.
- The main() method is static, so Java can start execution without creating an object of the class.
- The static keyword helps to group common logic in one place (like utility classes).
- Static is used with final keyword to create constants (like PI).
Types of Static Members in Java
There are four types of static members in Java”
- Static Variable
- Static Method
- Static Block
- Static Nested Class
Let’s understand each one by one with the help of basic syntax and examples.
Static Variable in Java
When you declare a variable with the static keyword, it is called a static variable or class variable in Java. It is created when the class is loaded into memory.
Unlike instance variables, which get memory allocated each time an object is created, a static variable gets memory only once per class. This allocation typically occurs in the method area (Metaspace in modern JVMs) when the class is loaded.
Key Features of Static Variable in Java
The key features of the static keyword are:
- A static variable belongs to the class, not to any specific object.
- There is only one copy in memory, shared among all instances of that class.
- Static variables are typically accessed using the class name (recommended practice).
Syntax to Declare Static Variable in Java
The basic syntax for declaring a variable with a static keyword in Java is:
class ClassName {
// Declaration of static variable.
static dataType variableName;
}
Example:
class Test {
static int count = 0;
}
Examples of Static Variables in Java
Let us take some important example programs based on the static variables in Java that will help to understand the behavior of static variables.
Example 1: Accessing Static Variable
class Test {
static int x = 10; // static variable
public static void main(String[] args) {
// Accessing static variable using class name.
System.out.println(Test.x);
}
}
Output:
10
Example 2:
class Student {
String name;
static String college = "ISM College";
Student(String name) {
this.name = name;
}
void display() {
System.out.println(name + " - " + college);
}
public static void main(String[] args) {
Student s1 = new Student("John");
Student s2 = new Student("Mark");
s1.display();
s2.display();
}
}
Output:
John - ISM College
Mark - ISM College
As you see in the above example code, both objects share the same college name.
Example 3: Object Counter
class Counter {
static int count = 0;
Counter() {
count++;
}
public static void main(String[] args) {
new Counter();
new Counter();
new Counter();
System.out.println("Total Objects: " + count);
}
}
Output:
Total Objects: 3
A static variable is shared by all objects, so every time a new object is created, the same variable is updated. Each time the constructor runs, the value increases. That’s why it can count total objects.
Static Method in Java
When you define a method with the static keyword, it is known as static method or class method in Java. It belongs to the class itself rather than to any specific object. This means you can invoke them directly using the class name, without the need to create an instance of the class first. The main() method is the best example of static method.
Key Features of Static Method in Java
The following are the key features of static methods in Java:
- Static methods belong to the class, not to the specific object.
- You can call them without creating an object of the class.
- You can access them using ClassName.methodName().
- A static method can directly access only static variables and methods
- You cannot use this or super keyword inside the static method.
Syntax to Declare Static Method in Java
The general syntax for declaring staic method in Java is:
class ClassName {
static returnType methodName(parameters) {
// method body
}
}
Example 4:
class StaticMethodExample {
// Declare a static method.
static void show() {
System.out.println("Hello Static Method");
}
public static void main(String[] args) {
// Accessing static method using class name.
Demo.show();
}
}
Output:
Hello Static Method
Example 5: Static Method With Return Value
class MathUtil {
static int square(int x) {
return x * x; // return statement
}
public static void main(String[] args) {
System.out.println("Square of 5: " + MathUtil.square(5));
}
}
Output:
Square of 5: 25
Example 6: Accessing Static Variable in Static Method
class Test {
// Declare static variable of type int.
static int x = 10;
// Declare static method.
static void display() {
System.out.println(x); // allowed
}
}
Output:
10
Example 7: Accessing Non-Static Variables in Static Method
class Demo {
// Declaration of non-static variable.
int x = 10;
static void show() {
// System.out.println(x); ERROR
}
}
You cannot access non-static (i.e., instance) variables from a static method.
Example 8: Accessing Non-Static Variables in Static Method
class Demo {
int x = 10;
static void show() {
// Create an object of class Demo.
Demo obj = new Demo();
// Access instance variable inside static method using reference variable.
System.out.println(obj.x); // Allowed using object.
}
}
Output:
10
Example 9: Calling Static Method from Another Static Method
class Demo {
static void method1() {
System.out.println("Method 1");
}
static void method2() {
method1(); // direct call
}
}
Output:
Method 1
Example 10: Import Static
import static java.lang.Math.*;
class Test {
public static void main(String[] args) {
System.out.println(sqrt(16)); // no class name
}
}
Output:
4.0
Conclusion: Static Keyword in Java
The static keyword in Java is a powerful feature that allows you to create class-level members shared across all objects of the class. Static members are loaded once when the class is loaded in the memory. In this tutorial, you learned static variables and static methods with various examples. Stay tuned for the next tutorial, where we will learn about static blocks in Java with examples.




