In this article Static keyword in Java we give the information about static keyword is mainly used for memory management. It can be used with variables, methods, blocks and nested classes.
Static Keyword in Java
The static keyword in Java is mainly used for memory management. It can be applied to:
- Variables
- Methods
- Blocks
- Nested Classes
When a member of a class is declared with static, it belongs to the class rather than any object of the class. This means:
- It can be accessed without creating an object.
- All objects share the same static member.
1. Static Variables
A static variable is also known as a class variable.
It is shared by all objects of the class and memory is allocated only once at the class level.
Important Points:
- Static variables are declared only inside a class (not inside a method or constructor).
- They are initialized only once.
- They save memory since only one copy exists for all objects.
Example: Static Variable
class Employee
{
int empid;
String name;
static String company = “TCS”; // static variable
Employee(int empid, String name)
{
this.empid = empid; this.name = name;
}
void display()
{
System.out.println(empid + ” ” + name + ” ” + company);
}
}
class StaticVariable
{
public static void main(String[] args)
{
Employee e1 = new Employee(101, “Ram”);
e1.display();
Employee e2 = new Employee(102, “Geeta”);
e2.display();
}
}
Output:
101 Ram TCS102 Geeta TCS
2. Static Methods
A static method belongs to the class rather than an object.
The most common example is the main() method.
Rules for Static Methods:
- They can be called using the class name (no object required).
- They can only access static data members.
- They can call only other static methods
- They cannot use this or super
Example 1: Static Method Accessing Static Data
class StaticDemo
{
static int n = 100;
public static void display()
{
System.out.println(“Value of N: ” + n);
}
public static void main(String[] args)
{
StaticDemo.display();
}
}
Output:
Value of N: 100
Example 2: Static Method Calling Another Static Method
class StaticMethod
{
public static void display()
{
show();
System.out.println(“Hello!”);
}
public static void show()
{
System.out.println(“Hi!”);
}
public static void main(String[] args)
{
StaticMethod.display();
}
}
Output:
Hi!Hello!
Example 3: Using this in Static Context (Error Example)
class StaticThis
{
int n = 100;
public static void show()
{
System.out.println(this.n); // Error
}
public static void main(String[] args)
{
StaticThis.show();
}
}
Output:
Error: non-static variable this cannot be referenced from a static context
3. Static Block
A static block is executed automatically when the class is loaded into memory — before the main() method.
Example: Static Block
public class StaticBlockExample
{
static
{
System.out.println(“Static block is executed before main method.”);
}
public static void main(String[] args)
{
System.out.println(“Main method is executed.”);
}
}
Output:
Static block is executed before main method.Main method is executed.
4. Static Nested Class
In Java, a class can be defined inside another class.
If it is declared with the static keyword, it is called a static nested class.
Example: Static Nested Class
public class OuterClass
{
static class StaticNestedClass
{
void display()
{
System.out.println(“Inside static nested class.”);
}
}
public static void main(String[] args)
{
OuterClass.StaticNestedClass obj = new OuterClass.StaticNestedClass();
obj.display();
}
}
Output:
Inside static nested class.
5. Example Combining Static and Non-Static Methods
class Area
{
void RectArea()
{
int l = 5, b = 4;
System.out.println(“Area of Rectangle: ” + (l * b));
}
public static void AreaSquare()
{
int s = 7;
System.out.println(“Area of Square: ” + (s * s));
}
}
public class AllArea
{
public static void main(String[] args)
{
Area obj = new Area(); // Call non-static method using object
obj.RectArea(); // Call static method using class name
Area.AreaSquare();
}
}
Output:
Area of Rectangle: 20Area of Square: 49
Summary
| Concept | Description |
| Static Variable | Shared by all objects; one copy only |
| Static Method | Belongs to the class; can access only static members |
| Static Block | Executes automatically when class loads |
| Static Class | Nested class declared with static keyword |
| Main Advantage | Saves memory and allows easy access to class-level members |
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining