Skip to content

Latest commit

 

History

History
1539 lines (882 loc) · 20.9 KB

File metadata and controls

1539 lines (882 loc) · 20.9 KB

Static in Java — DEEPDIVE

static is one of the most important Java keywords because it explains the difference between class-level members and object-level members.


  1. What is static?

static is a Java keyword used to declare a member that belongs to the class, rather than being associated with each individual object.

It can be used with:

Variables

Methods

Initialization blocks

Nested classes

For the core Java concepts, think:

Class | static member | shared class-level state

Whereas an instance member belongs to an object:

Class ├── Object 1 → instance members ├── Object 2 → instance members └── Object 3 → instance members


  1. Why Do We Need static?

Suppose we have:

class Student {

int rollNo;
String name;
String college;

}

Suppose 1,000 students belong to the same college.

Each object stores its own:

rollNo name college

But college is common.

Instead:

class Student {

int rollNo;
String name;

static String college;

}

Now:

rollNo → individual name → individual college → shared

This is the basic reason static exists.


  1. Instance Variable vs Static Variable

Consider:

class Student {

int rollNo;
static String college;

}

Create objects:

Student s1 = new Student(); Student s2 = new Student();

Conceptually:

Student Class | static college | "ABC College" | ┌─────────┴─────────┐ ↓ ↓ s1 s2 rollNo=101 rollNo=102

There is one shared college value.

But:

s1.rollNo s2.rollNo

are separate.


  1. Java Program WITHOUT Static Variable

class Student {

int count = 0;

Student() {
    count++;
    System.out.println("Count = " + count);
}

public static void main(String[] args) {

    Student s1 = new Student();
    Student s2 = new Student();
    Student s3 = new Student();
}

}

Output

Count = 1 Count = 1 Count = 1

Why?

Every object gets a separate count.

s1 → count = 1 s2 → count = 1 s3 → count = 1


  1. Same Program WITH Static Variable

class Student {

static int count = 0;

Student() {
    count++;
    System.out.println("Count = " + count);
}

public static void main(String[] args) {

    Student s1 = new Student();
    Student s2 = new Student();
    Student s3 = new Student();
}

}

Output

Count = 1 Count = 2 Count = 3

Now there is one shared count.

Student | static count | 3 / |
s1 s2 s3


  1. Static Variable Is Also Called Class Variable

A static variable is commonly called a:

Class variable

because it belongs to the class.

Example:

class Employee {

int id;                 // instance variable
static String company; // class variable

}


  1. How Should We Access a Static Variable?

You can sometimes access a static member through an object:

Student s = new Student();

s.college = "ABC";

But this is not the preferred style.

Use the class name:

Student.college = "ABC";

This makes it clear that college belongs to the class.

Recommended:

ClassName.staticMember

Example:

Student.college Student.count Student.displayCollege()


  1. Static Variable Initialization

A static variable can be initialized directly:

class Demo {

static int x = 100;

public static void main(String[] args) {
    System.out.println(x);
}

}

Output:

100


  1. Static Variable Without Explicit Initialization

class Demo {

static int x;

public static void main(String[] args) {
    System.out.println(x);
}

}

Output:

0

Static variables receive default values if no explicit initializer is provided.

Examples:

Type Default

int 0 long 0L float 0.0f double 0.0d char '\u0000' boolean false Reference null


  1. Static Method

A method declared using static is a static method.

Syntax:

static returnType methodName(parameters) { // body }

Example:

class Calculator {

static int add(int a, int b) {
    return a + b;
}

public static void main(String[] args) {

    int result = Calculator.add(10, 20);

    System.out.println(result);
}

}

Output:

30

No Calculator object was required to call add().


  1. Why Can We Call a Static Method Without an Object?

Because the method belongs to the class.

Calculator.add(10, 20);

Conceptually:

Calculator Class | └── static add()

The method isn't waiting for a particular object.


  1. Instance Method vs Static Method

Instance method

class Demo {

void display() {
    System.out.println("Hello");
}

public static void main(String[] args) {

    Demo d = new Demo();

    d.display();
}

}

We create an object.


Static method

class Demo {

static void display() {
    System.out.println("Hello");
}

public static void main(String[] args) {

    Demo.display();
}

}

No object is required.


  1. The Most Important Static Method Rule

A static method can directly access static members.

class Demo {

static int x = 100;

static void display() {

    System.out.println(x);
}

public static void main(String[] args) {
    display();
}

}

Output:

100

Why?

Both belong to the class.

Class ├── static x └── static display()


  1. Can Static Method Directly Access Instance Variable?

No.

class Demo {

int x = 100;

static void display() {

    System.out.println(x); // ERROR
}

}

Why?

x belongs to an object.

But display() can execute without an object.

There is no particular object whose x should be used.


  1. How Can Static Method Access Instance Variable?

Create/use an object:

class Demo {

int x = 100;

static void display() {

    Demo d = new Demo();

    System.out.println(d.x);
}

public static void main(String[] args) {
    display();
}

}

Output:

100

Now the object is explicitly identified.


  1. Can Static Method Directly Call Instance Method?

No.

class Demo {

void test() {
    System.out.println("Test");
}

static void display() {

    test(); // ERROR
}

}

Correct:

static void display() {

Demo d = new Demo();

d.test();

}


  1. Why Can't Static Directly Access Instance Members?

This is the fundamental question.

Consider:

class Demo {

int x;

static void display() {
    System.out.println(x);
}

}

Suppose there are:

Demo d1 = new Demo(); Demo d2 = new Demo();

Which x should display() use?

d1.x ? d2.x ?

There is no object reference.

Therefore Java doesn't allow direct access.


  1. Static Method and this

Can we use:

this

inside a static method?

❌ No.

Example:

class Demo {

int x = 10;

static void display() {

    System.out.println(this.x); // ERROR
}

}

Why?

this means:

Reference to the current object.

A static method has no implicit current object.


  1. Static Method and super

Similarly, super represents the current object's superclass portion.

So you cannot use it as an instance reference from a static context.


  1. Static Block

A static block is declared as:

static { // statements }

Example:

class Demo {

static {
    System.out.println("Static Block");
}

public static void main(String[] args) {

    System.out.println("Main Method");
}

}

Output:

Static Block Main Method


  1. Why Does Static Block Execute Before main()?

When a class is initialized, its static initialization occurs before the class's main() execution in the usual launch scenario.

Example:

class Demo {

static {
    System.out.println("1");
}

public static void main(String[] args) {

    System.out.println("2");
}

}

Output:

1 2


  1. Multiple Static Blocks

Yes, Java allows multiple static blocks.

class Demo {

static {
    System.out.println("Static Block 1");
}

static {
    System.out.println("Static Block 2");
}

static {
    System.out.println("Static Block 3");
}

public static void main(String[] args) {

    System.out.println("Main");
}

}

Output:

Static Block 1 Static Block 2 Static Block 3 Main

They execute in source-code order.


  1. Static Block + Static Variable

class Demo {

static int x;

static {
    x = 100;
}

public static void main(String[] args) {

    System.out.println(x);
}

}

Output:

100

The static block initializes the static variable.


  1. Why Use Static Block?

Static blocks are useful when class-level initialization requires multiple statements or logic, rather than a simple declaration initializer.

Example:

class Database {

static String url;

static {
    url = "jdbc:mysql://localhost/test";
    System.out.println("Database configuration initialized");
}

public static void main(String[] args) {
    System.out.println(url);
}

}


  1. Static Block vs Constructor

This is an important distinction.

Static block

static { // class initialization }

Runs when the class is initialized.

Constructor

Demo() { // object initialization }

Runs when an object is created.

Example:

class Demo {

static {
    System.out.println("Static Block");
}

Demo() {
    System.out.println("Constructor");
}

public static void main(String[] args) {

    System.out.println("Main");

    Demo d1 = new Demo();
    Demo d2 = new Demo();
}

}

Output:

Static Block Main Constructor Constructor

Notice:

Static Block → once for class initialization Constructor → once per object creation


  1. Static Block vs Instance Block

Java can also have an instance initialization block:

{ System.out.println("Instance Block"); }

Compare:

class Demo {

static {
    System.out.println("Static Block");
}

{
    System.out.println("Instance Block");
}

Demo() {
    System.out.println("Constructor");
}

public static void main(String[] args) {

    System.out.println("Main");

    Demo d = new Demo();
}

}

Output:

Static Block Main Instance Block Constructor

The instance block runs as part of object construction, before the constructor body.


  1. Static Initialization Order

Consider:

class Demo {

static int x = 10;

static {
    System.out.println("Block 1: " + x);
}

static int y = 20;

static {
    System.out.println("Block 2: " + y);
}

public static void main(String[] args) {

    System.out.println("Main");
}

}

Output:

Block 1: 10 Block 2: 20 Main

Static field initializers and static blocks execute in textual order during class initialization.


  1. Static Variable and Memory — Important Correction

You may hear:

"Static variables are stored in the Method Area."

This is a useful traditional teaching model, but don't treat it as a universal physical-memory rule.

The Java specification defines behavior and class/runtime areas abstractly; exact physical memory organization is JVM implementation-specific.

For exam-level understanding:

static variable → class-level data → associated with the class

That's more important than claiming a particular physical memory location.


  1. Static Doesn't Mean "Constant"

This is a very common mistake.

static int x = 10;

does not mean x cannot change.

x = 20;

is perfectly valid.

If you want a class-level constant, commonly use:

static final int MAX = 100;

Here:

static → class-level final → cannot be reassigned


  1. static final

Example:

class Constants {

static final double PI = 3.14159;

public static void main(String[] args) {

    System.out.println(PI);
}

}

Preferred access:

Constants.PI

This represents a class-level constant.


  1. Can a Static Variable Be Modified?

Yes.

class Demo {

static int x = 10;

public static void main(String[] args) {

    System.out.println(x);

    x = 50;

    System.out.println(x);
}

}

Output:

10 50

static does not mean constant.


  1. Can We Overload Static Methods?

Yes.

Static methods can be overloaded.

class Demo {

static void display() {
    System.out.println("No argument");
}

static void display(int x) {
    System.out.println("Integer");
}

static void display(String s) {
    System.out.println("String");
}

public static void main(String[] args) {

    display();
    display(10);
    display("Java");
}

}

Output:

No argument Integer String

static does not prevent method overloading.


  1. Can Static Methods Be Overridden?

This requires careful terminology.

A static method is not overridden in the runtime-polymorphic sense. If a subclass declares a static method with the same signature, the method is hidden, not overridden.

Example:

class Parent {

static void display() {
    System.out.println("Parent");
}

}

class Child extends Parent {

static void display() {
    System.out.println("Child");
}

}

Then:

Parent p = new Child();

p.display();

Output:

Parent

Why?

Static method selection is based on the reference type, not runtime object type.

This is called method hiding.


  1. Static Method Hiding vs Instance Method Overriding

Instance method

Runtime object determines method

Static method

Reference/class determines method

Therefore:

Instance method → overriding Static method → hiding


  1. Can We Have Static Constructor?

❌ No.

Constructors are associated with object creation.

A constructor cannot be declared:

static Demo() { } // ERROR

Static members belong to the class, but constructors initialize objects.


  1. Can We Access Static Members Through an Object?

Technically, Java allows access to static members through an instance in many cases:

class Demo {

static int x = 100;

public static void main(String[] args) {

    Demo d = new Demo();

    System.out.println(d.x);
}

}

But preferred:

System.out.println(Demo.x);

Why?

Because x belongs to the class, not specifically to d.


  1. Can We Have Static Local Variables?

❌ No.

Java does not support C/C++-style static local variables.

This is invalid:

void test() {

static int x = 10; // ERROR

}

Java's static is used for class-level members and certain nested types, not local variables.


  1. Can an Instance Method Access Static Members?

Yes.

class Demo {

static int x = 100;

void display() {
    System.out.println(x);
}

public static void main(String[] args) {

    Demo d = new Demo();

    d.display();
}

}

Output:

100

Why?

An instance method has an object context, and the class's static members are also accessible.


  1. Can an Instance Method Access Both?

Yes.

class Demo {

static int x = 100;
int y = 200;

void display() {

    System.out.println(x);
    System.out.println(y);
}

public static void main(String[] args) {

    Demo d = new Demo();

    d.display();
}

}

Output:

100 200


  1. Static Nested Class

A nested class can also be declared static.

class Outer {

static class Inner {

    void display() {
        System.out.println("Inner class");
    }
}

public static void main(String[] args) {

    Outer.Inner obj = new Outer.Inner();

    obj.display();
}

}

Important:

A top-level class cannot be declared static.

static nested classes are a separate advanced concept from static variables/methods.


  1. Why Is main() Static?

The standard entry point is:

public static void main(String[] args)

The important reason for static is:

The JVM can invoke the entry-point method without first creating an instance of the application class.

Conceptually:

JVM ↓ loads/initializes class as needed ↓ calls static main()

No application object is required merely to invoke main().


  1. Complete Program — All Important Concepts

Here is one program combining:

Instance variable

Static variable

Static block

Constructor

Instance method

Static method

main()

class Student {

// Instance variable
int rollNo;
String name;

// Static variable
static String college;

// Static block
static {
    college = "ABC College";
    System.out.println("Static block executed");
}

// Constructor
Student(int rollNo, String name) {
    this.rollNo = rollNo;
    this.name = name;
}

// Instance method
void displayStudent() {
    System.out.println("Roll No : " + rollNo);
    System.out.println("Name    : " + name);
    System.out.println("College : " + college);
}

// Static method
static void displayCollege() {
    System.out.println("College : " + college);
}

public static void main(String[] args) {

    System.out.println("Main method started");

    // Static method
    Student.displayCollege();

    // Objects
    Student s1 = new Student(101, "Ravi");
    Student s2 = new Student(102, "Kiran");

    // Instance methods
    s1.displayStudent();
    s2.displayStudent();
}

}

Output

Static block executed Main method started College : ABC College

Roll No : 101 Name : Ravi College : ABC College

Roll No : 102 Name : Kiran College : ABC College


  1. Understanding the Complete Program

Step 1 — Class initialization

static { college = "ABC College"; }

The static initialization runs when the class is initialized.


Step 2 — main()

public static void main(String[] args)

Execution begins through the static entry point.


Step 3 — Static method

Student.displayCollege();

No object required.


Step 4 — Object creation

Student s1 = new Student(...); Student s2 = new Student(...);

Now each object gets its own:

rollNo name

But both share:

college


  1. Static vs Instance — Complete Comparison

Feature Static Instance

Belongs to Class Object Number of copies Generally one class-level member One per object Object required? Not for access/invocation Normally yes Keyword static No static Can access static members Yes Yes Can directly access instance members No Yes this available? No Yes super instance reference available? No Yes Example Student.college student.rollNo


  1. 🔥 Static Execution Flow

A simplified mental model:

Java Class | ↓ Class initialization | ┌───────┴────────┐ ↓ ↓ Static field Static blocks initializers (textual order) └───────┬────────┘ ↓ main() ↓ Object creation ↓ Constructors ↓ Instance methods

Don't confuse this with a universal internal JVM implementation sequence for every possible situation; it's the useful model for a normal program launched through main().


  1. Common Static Mistakes

❌ Mistake 1

static int x;

and assuming every object gets its own x.

Wrong.

It is shared at the class level.


❌ Mistake 2

Thinking:

static

means:

constant

Wrong.

Use:

static final

for a class-level constant.


❌ Mistake 3

Trying to use:

this

inside static method.

Wrong.


❌ Mistake 4

Trying to directly access an instance variable from static method.

Wrong.


❌ Mistake 5

Thinking a static method is overridden.

More precisely: static methods are hidden, not overridden.


❌ Mistake 6

Thinking Java supports:

static int x;

inside a method.

Wrong. Java doesn't have static local variables.


  1. 🧠 Static — Deep Revision

Remember this hierarchy:

STATIC | ┌─────────────────┼─────────────────┐ ↓ ↓ ↓ Variable Method Block ↓ ↓ ↓ Shared state Class-level Class-level behavior initialization

Static variable

One class-level copy

Static method

Can execute without an object

Static block

Runs during class initialization


🏆 Final Static Formula

static means class-level association. A static variable is shared by objects of the class, a static method can be invoked without an object and cannot directly use instance state, and a static block performs class-level initialization. static does not mean constant—final is what prevents reassignment.

The 5 rules you should never forget

  1. static variable → shared class-level state

  2. static method → no implicit object / no this

  3. static method → cannot directly access instance members

  4. static block → class initialization

  5. static final → class-level constant