1. Introduction
In Java, static and non-static are important concepts in Object-Oriented Programming (OOP).
They help us understand how variables and methods belong to a class or an object.
Java provides the static keyword to declare class-level members.
There are two categories:
- Static members
- Non-static (instance) members
2. What is Static in Java?
Static means a member belongs to the class rather than an individual object.
We use the static keyword to declare static variables, methods, and blocks.
Example: Static Variable
class Student {
static String college = "ABC College";
public static void main(String[] args) {
System.out.println(Student.college);
}
}
Output:
ABC College
Explanation
-
collegeis a static variable. - It belongs to the
Studentclass. - All Student objects share the same static variable.
- We can access it using the class name without creating an object.
Student.college;
3. What is Non-Static in Java?
Non-static members belong to individual objects.
A variable or method declared without the static keyword is generally an instance member.
Example: Non-Static Variable
class Student {
String name;
int age;
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Abishek";
s1.age = 21;
System.out.println(s1.name);
System.out.println(s1.age);
}
}
Output:
Abishek
21
Explanation
-
nameandageare instance variables. - They belong to the Student object.
- Each object gets its own copy of these instance variables.
- We need an object reference to access them.
4. Real-World Example: College Management System
Imagine a college with 1,000 students.
Every student has:
- A different name.
- A different age.
- A different roll number.
- The same college name.
We can represent this using static and non-static variables.
| Student | Name | Age | College |
|---|---|---|---|
| Student 1 | Abishek | 21 | ABC College |
| Student 2 | Arun | 22 | ABC College |
| Student 3 | Kumar | 20 | ABC College |
Here:
- Name → Non-static
- Age → Non-static
- Roll number → Non-static
- College name → Static
Java Implementation
class Student {
String name;
int age;
static String college = "ABC College";
void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("College: " + college);
}
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
s1.name = "Abishek";
s1.age = 21;
s2.name = "Arun";
s2.age = 22;
s1.display();
s2.display();
}
}
Output:
Name: Abishek
Age: 21
College: ABC College
Name: Arun
Age: 22
College: ABC College
5. Static Method in Java
A method declared using the static keyword is called a static method.
Static methods belong to the class and can be called without creating an object.
Example
class Calculator {
static void add() {
int a = 10;
int b = 20;
System.out.println(a + b);
}
public static void main(String[] args) {
Calculator.add();
}
}
Output:
30
Here, we call the static method using the class name.
Calculator.add();
We don't need to create an object.
6. Non-Static Method in Java
A method declared without the static keyword is called a non-static method or instance method.
To call a non-static method, we need an object.
Example
class Calculator {
void multiply() {
int a = 10;
int b = 20;
System.out.println(a * b);
}
public static void main(String[] args) {
Calculator c1 = new Calculator();
c1.multiply();
}
}
Output:
200
Here:
-
multiply()is a non-static method. -
c1is an object of the Calculator class. - We use the object reference to call the method.
7. Difference Between Static and Non-Static
| Feature | Static | Non-Static |
|---|---|---|
| Belongs to | Class | Object |
| Keyword | static |
No static keyword |
| Object required? | No | Yes, for instance access |
| Variables | Shared among objects | Separate for each object |
| Methods | Can be called using class name | Called using object reference |
| Example | static int count; |
int age; |
8. Why Is the Main Method Static in Java?
This is one of the most frequently asked Java interview questions.
Consider this program:
class Student {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
When we execute:
javac Student.java
java Student
We get:
Hello Java
But we never created a Student object to call the main() method.
Why?
Because the main method is static.
Explanation
The Java launcher starts the application and invokes its entry-point method.
Since main() is static, it can be invoked without creating an object of the Student class.
If the main method were non-static, an object would be required to call it.
For example:
Student s1 = new Student();
s1.main(args);
However, the traditional Java application launcher expects a valid static main method as its entry point.
Therefore, we declare:
public static void main(String[] args)
Meaning of Each Keyword
| Keyword | Explanation |
|---|---|
public |
Allows the launcher to access the method |
static |
Allows invocation without creating an object |
void |
Does not return any value |
main |
Name of the traditional application entry-point method |
String[] args |
Receives command-line arguments |
9. How Does the JVM Execute the Main Method?
When we execute:
java Student
The Java application startup process happens approximately as follows:
- The Java launcher starts the runtime.
- The JVM loads the required class.
- The class is linked and initialized.
- The launcher identifies the valid
main()entry point. - The launcher invokes the main method.
- Statements inside the main method execute.
Execution Flow
Java Source Code
|
v
javac Compiler
|
v
Bytecode (.class)
|
v
java Command
|
v
JVM
|
v
Class Loading
|
v
Class Initialization
|
v
Main Method
|
v
Program Execution
10. Why Can't a Static Method Directly Access a Non-Static Variable?
Consider this example:
class Student {
String name = "Abishek";
public static void main(String[] args) {
System.out.println(name);
}
}
This code produces a compilation error.
Why?
-
main()is a static method. -
nameis a non-static variable. - The non-static variable belongs to an object.
- The static method does not automatically have an object reference.
Correct Approach
class Student {
String name = "Abishek";
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(s1.name);
}
}
Output:
Abishek
Important: A static method can access non-static members through an object reference.
11. Static Block in Java
A static block is a block of code declared using the static keyword.
It executes during class initialization, before the main method is invoked when that class is the application entry point.
Example
class Employee {
static {
System.out.println("Static Block");
}
public static void main(String[] args) {
System.out.println("Main Method");
}
}
Output:
Static Block
Main Method
Explanation
During class initialization, Java executes static field initializers and static blocks in their textual order.
After class initialization, the launcher invokes the main method.
Another Example
class Test {
static int a = 10;
static {
a = 20;
}
public static void main(String[] args) {
System.out.println(a);
}
}
Output:
20
Explanation:
- Static variable
ais initialized to 10. - Static block changes
ato 20. - Main method prints the value.
- Final output is 20.
12. Important Rules to Remember
- Static members belong to the class.
- Non-static members belong to individual objects.
- Static variables are shared among objects of the same class.
- Each object has its own instance variables.
- Static methods can directly access static members.
- Static methods cannot directly access instance members without an object reference.
- Non-static methods can access both static and non-static members.
- The traditional Java main method is static so the launcher can invoke it without creating an instance.
- Static blocks execute during class initialization.
13. Conclusion
Static and non-static are fundamental concepts in Java.
Static members are useful when data or functionality is common to all objects of a class. Non-static members are useful when each object needs its own data or behavior.
Understanding these concepts helps us write better Java programs and understand how the JVM starts and executes a Java application.
Top comments (0)