Static Methods and Variables in Java
In Core Java, the keyword static is used for class-level members — meaning they belong to the class itself, not to any specific object.
It’s one of the most important concepts in object-oriented programming, especially when you want shared data or utility methods.
What is static in Java?
The static keyword in Java is used for members (variables, methods, blocks, or nested classes) that belong to the class rather than to instances (objects).
In simple words:
- Static members are shared among all objects of a class.
- You can access them without creating an object.
Static Variables (Class Variables)
A static variable is a variable that is common to all objects of a class.
It’s created only once in memory, when the class is loaded.
Example: Static Variable
class Employee {
int id;
String name;
static String company = "TechCorp"; // static variable (shared)
Employee(int id, String name) {
this.id = id;
this.name = name;
}
void display() {
System.out.println(id + " " + name + " " + company);
}
}
public class Main {
public static void main(String[] args) {
Employee e1 = new Employee(101, "Alice");
Employee e2 = new Employee(102, "Bob");
e1.display();
e2.display();
// Changing static variable affects all objects
Employee.company = "CodeWorks";
e1.display();
e2.display();
}
}
Output
101 Alice TechCorp
102 Bob TechCorp
101 Alice CodeWorks
102 Bob CodeWorks
All objects share the same static variable company.
Key Points about Static Variables
| Feature | Description |
|---|---|
| Memory | Allocated once when the class is loaded. |
| Shared | Common for all objects. |
| Access | Can be accessed via class name (ClassName.variable). |
| Lifecycle | Exists until the class is unloaded by JVM. |
Static Methods
- A static method belongs to the class, not an instance.
- You can call it without creating an object.
Example: Static Method
class MathUtils {
static int add(int a, int b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
// Calling static method using class name
int result = MathUtils.add(5, 10);
System.out.println("Sum: " + result);
}
}
Output
Sum: 15
Rules for Static Methods
- Can access only static variables and methods directly.
- Cannot access non-static (instance) members directly.
- Can be called without creating an object.
- Commonly used for utility or helper functions.
Example: Static vs Non-Static
class Demo {
int x = 10;
static int y = 20;
static void showStatic() {
// System.out.println(x); // Cannot access instance variable
System.out.println("Static y = " + y);
}
void showInstance() {
System.out.println("Instance x = " + x);
System.out.println("Static y = " + y);
}
}
public class Main {
public static void main(String[] args) {
Demo.showStatic(); // Access static method directly
Demo obj = new Demo();
obj.showInstance(); // Access instance method via object
}
}
Output
Static y = 20
Instance x = 10
Static y = 20
Static Blocks
- A static block is used to initialize static variables.
- It runs once when the class is loaded.
Example: Static Block
class Test {
static int num;
// static block
static {
System.out.println("Static block executed");
num = 100;
}
public static void display() {
System.out.println("Number = " + num);
}
}
public class Main {
public static void main(String[] args) {
Test.display();
}
}
Output
Static block executed
Number = 100
Static block runs before any static method or object creation.
Static Nested Classes
- A static nested class is a class declared inside another class with the
statickeyword. - It can access only static members of the outer class.
Example
class Outer {
static int data = 50;
static class Inner {
void show() {
System.out.println("Data: " + data);
}
}
}
public class Main {
public static void main(String[] args) {
Outer.Inner obj = new Outer.Inner(); // No need for Outer object
obj.show();
}
}
Output
Data: 50
Why Use static in Java?
- To save memory — shared variable instead of duplicates.
- To access methods or constants without creating objects.
- To build utility/helper classes (e.g.,
Math,Collections).
Static vs Instance Members
| Feature | Static | Instance |
|---|---|---|
| Belongs To | Class | Object |
| Memory | Shared (once per class) | Separate for each object |
| Access | Via Class name | Via Object |
| Lifecycle | Exists till class is loaded | Exists till object is alive |
| Example | Math.max() | obj.toString() |
Points to Remember
staticmeans shared and belongs to the class.- Static variables → single copy shared by all objects.
- Static methods → called without object creation.
- Static blocks → used for class-level initialization.
- Can’t access instance data directly from static context.
