static Keyword in Java
The static keyword in Java is used for memory management.
● It can be applied to variables, methods, blocks, and nested classes.
● When something is declared static, it belongs to the class rather than to any object.
● It is loaded only once when the class is loaded into memory.
1. Static Variable
● Also called class variable.
● Value of static variable is shared among all objects.
● Memory is allocated only once (not separately for each object).
Example:
class Student {
int rollno; // instance variable
String name;
static String college = "ABC College"; // static variable
Student(int r, String n) {
rollno = r;
name = n;
}
void display() {
[Link](rollno + " " + name + " " + college);
}
}
class TestStaticVariable {
public static void main(String args[]) {
Student s1 = new Student(101, "Priya");
Student s2 = new Student(102, "Aman");
[Link]();
[Link]();
}
}
Output:
101 Priya ABC College
102 Aman ABC College
Use: All students belong to the same college, so we use a static variable.
2. Static Method
● A method declared with static can be called without creating an object.
● It can access only static variables directly (not instance variables).
● To access instance variables, you need to create an object.
Example:
class MathUtil {
static int square(int x) { // static method
return x * x;
}
}
class TestStaticMethod {
public static void main(String args[]) {
[Link]("Square of 5: " + [Link](5)); // no object needed
}
}
Output:
Square of 5: 25
Use: For utility/helper methods like sqrt(), max(), etc. (just like in Math class).
3. Static Block
● A block of code inside static { } is called static block.
● Runs only once when the class is loaded in memory.
● Useful for initialization of static data.
Example:
class Demo {
static int count;
// static block
static {
count = 100;
[Link]("Static block executed");
}
public static void main(String args[]) {
[Link]("Value of count: " + count);
}
}
Output:
Static block executed
Value of count: 100
Use: Initialize static variables or run some setup code only once.
4. Static Class (Nested Class)
● A class inside another class can be made static.
● Known as static nested class.
● It can access only static members of the outer class directly.
Example:
class Outer {
static int data = 50;
// static nested class
static class Inner {
void msg() {
[Link]("Data is: " + data);
}
}
}
class TestStaticClass {
public static void main(String args[]) {
[Link] obj = new [Link](); // no need of Outer object
[Link]();
}
}
Output:
Data is: 50
Use: To group classes logically, and when inner class does not need reference to outer object.
Why static is Needed?
1. Memory efficient → common data is stored only once.
2. Code reuse → static methods can be called without objects.
3. Initialization → static blocks run once for setup.
4. Logical grouping → static nested class helps in organizing code.
So, static is mainly used for shared values, utility methods, initialization, and logical
grouping of classes.
Rules of Static Methods with Examples
Rule 1: Static methods belong to class, not object
class Demo1 {
static void show() { // static method
[Link]("Static method belongs to class, not object");
public static void main(String args[]) {
[Link](); // called using class name (preferred way)
Demo1 obj = new Demo1();
[Link](); // also possible, but not recommended
Output:
Static method belongs to class, not object
Static method belongs to class, not object
Rule 2: Static methods can be accessed directly without
object
class Demo2 {
static void greet() {
[Link]("Hello! Called without creating an object");
public static void main(String args[]) {
greet(); // directly
[Link](); // using class name
Output:
Hello! Called without creating an object
Hello! Called without creating an object
Rule 3: Static method can access only static data (not
instance data)
class Demo3 {
int roll = 101; // instance variable
static String college = "ABC College"; // static variable
static void show() {
❌ ERROR - instance data not allowed
// [Link](roll);
[Link](college); // ✅ only static data allowed
public static void main(String args[]) {
show();
Output:
ABC College
Rule 4: Static method can call only other static methods
class Demo4 {
static void method1() {
[Link]("Static method1 called");
void method2() {
[Link]("Non-static method2 called");
}
static void test() {
method1(); // ✅ allowed
// method2(); ❌ ERROR - non-static method cannot be called
public static void main(String args[]) {
test();
Output:
Static method1 called
Rule 5: Static method cannot use this or super
class Parent {
void display() {
[Link]("Parent display");
class Demo5 extends Parent {
int x = 10;
static void test() {
// [Link](this.x); ❌ ERROR: Cannot use this
// [Link](); ❌ ERROR: Cannot use super
[Link]("Static method cannot use this/super");
public static void main(String args[]) {
test();
Output:
Static method cannot use this/super
All rules in one program:
class Student {
int roll; // instance variable (non-static)
String name;
static String college = "ABC"; // static variable (common for all)
Student(int r, String n) {
roll = r;
name = n;
// Rule 1 & 2: Static method belongs to class, can be called without object
static void changeCollege(String newCollege) {
college = newCollege;
[Link]("College changed to: " + college);
// Non-static method (needs object)
void display() {
[Link](roll + " " + name + " " + college);
// Rule 3 & 4: Static method can access only static data/methods
static void showInfo() {
// [Link](roll); ❌ ERROR: cannot access instance variable
[Link]("College (static data): " + college);
❌ ERROR: cannot call non-static method
// display();
showMsg(); // ✅ can call another static method
static void showMsg() {
[Link]("Hello from static method!");
}
// Rule 5: Cannot use this/super inside static method
static void checkThisSuper() {
// [Link]([Link]); ❌ ERROR
// [Link](); ❌ ERROR
[Link]("Static method cannot use this or super");
class TestStatic {
public static void main(String args[]) {
// Rule 1 & 2: Call static method without object
[Link]("XYZ University");
// Create objects
Student s1 = new Student(101, "Priya");
Student s2 = new Student(102, "Aman");
// Display students
[Link]();
[Link]();
// Rule 3 & 4: Static method accessing only static data
[Link]();
// Rule 5: Demonstrating restriction of this/super
[Link]();
These rules make static very powerful for memory efficiency, utility methods, and
initialization.