Clean • Professional
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.
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:
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.
| 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. |
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
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
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 keyword.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
Math, Collections).| 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() |
static means shared and belongs to the class.