Chapter 31 of 57

Static Keyword in Java

When working with classes and objects, we normally create an object to access its variables and methods. But sometimes we want something to belong to the class itself instead of individual objects.

This is where the static keyword comes in.

In simple words, static means that a member belongs to the class, rather than to a particular object.

Static Variable

Let's first look at a normal variable:

class Student {
    String name;
}

If we create two objects:

Student student1 = new Student();
Student student2 = new Student();

student1.name = "John";
student2.name = "Jason";

Each object has its own name.

student1 → John
student2 → Jason

But suppose we want to store something that is common to all students, such as the name of the school.

We could make it static:

class Student {
    String name;
    static String school = "ABC School";
}

Now school belongs to the Student class rather than each individual object.

We can access it using the class name:

System.out.println(Student.school);

Output:

ABC School

There is no need to create a Student object just to access school.

Static Variables Are Shared

A static variable is shared by all objects of that class.

For example:

class Student {
    String name;
    static String school = "ABC School";
}

We can create two students:

Student student1 = new Student();
Student student2 = new Student();

student1.name = "John";
student2.name = "Jason";

Both objects can access the same school variable:

System.out.println(student1.school);
System.out.println(student2.school);

Both will produce:

ABC School

There is one shared school value rather than a separate copy for every student.

Static Method

We can also make a method static.

For example:

class Calculator {

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

Because add() is static, we can call it using the class name:

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

System.out.println(result);

Output:

30

We don't need to create a Calculator object just to use this method.

This is useful for methods that don't need any information from a particular object.

Why Is main() Static?

You have already been writing:

public static void main(String[] args)

Now you can understand one part of it.

The main() method is static, which means Java can call it without first creating an object of the class.

When your program starts, Java needs an entry point. It can directly call the static main() method.

For example:

class Main {
    public static void main(String[] args) {
        System.out.println("Hello Java!");
    }
}

Java doesn't need us to write:

Main object = new Main();

before running main().

Static vs Non-Static

Let's compare them.

class Student {
    String name;
    static String school = "ABC School";
}

Here:

name   → belongs to each object
school → belongs to the class

So:

Student student1 = new Student();
Student student2 = new Student();

student1.name = "John";
student2.name = "Jason";

gives each object a different name.

But both share:

Student.school

This is the basic difference between instance members and static members.

Static Method Cannot Directly Access Instance Variables

There is an important rule you should know.

A static method belongs to the class, so it cannot directly access a non-static instance variable.

For example:

class Student {
    String name;

    static void showName() {
        System.out.println(name); // Error
    }
}

This doesn't work because name belongs to an object, while showName() belongs to the class.

Java doesn't know which object's name you want.

However, a static method can directly access another static member:

class Student {
    static String school = "ABC School";

    static void showSchool() {
        System.out.println(school);
    }
}

This works because both school and showSchool() belong to the class.

Static Block

Java also allows us to create a static block.

A static block is executed when the class is loaded.

For example:

class Main {

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

    public static void main(String[] args) {
        System.out.println("Main method executed.");
    }
}

Output:

Static block executed.
Main method executed.

Static blocks are generally used for initialization that needs to happen when the class is loaded. You won't need them very often as a beginner, but you'll encounter them as you learn more advanced Java.

A Simple Example

Let's put static variables and methods together:

class Student {
    String name;
    static String school = "ABC School";

    static void showSchool() {
        System.out.println("School: " + school);
    }
}

class Main {
    public static void main(String[] args) {

        Student student = new Student();
        student.name = "John";

        System.out.println("Name: " + student.name);

        Student.showSchool();
    }
}

Output:

Name: John
School: ABC School

Here, name belongs to the individual student object, while school and showSchool() belong to the Student class.

The easiest way to remember static is:

Non-static → belongs to an object

Static → belongs to the class

Once you understand this difference, you'll start seeing why some Java methods and variables can be accessed directly using the class name while others require an object.