Java static Modifier

What Is static?

static is a modifier that can be applied to class elements, such as methods and fields. Fields and methods given this modifier are allocated memory when the program runs and remain until the program ends. Their values are shared by all instances.

static Fields

Normal fields are newly created each time an instance is created and can have different values. A field declared with static is created when the program runs and is shared by all instances.

First, suppose there is a class called Student as follows.

Student.java

package com.devkuma.basic.statickeyword.ex1;

public class Student {
    static int number = 100;
    String name;

    Student(String name) {
        this.name = name;
    }
}

In this Student class, the number variable is declared as static.

StudentSample1.java

package com.devkuma.basic.statickeyword.ex1;

public class StudentSample {

    public static void main(String[] args) {
        Student devkuma = new Student("devkuma");
        Student araikuma = new Student("araikuma");
        Student kimkc = new Student("kimkc");

        System.out.println(++devkuma.number);
        System.out.println(++araikuma.number);
        System.out.println(++kimkc.number);
    }
}

Execution result:

101
102
103

In the example above, instances of the Student class are created and the number value of each instance is printed.

Each instance prints 101, 102, and 103. This is because all instances share the static field number. In other words, as shown in the figure below, three instances are looking at one field.

Sharing a static variable

Because static fields are created at a different time from instances, they can be referenced by the class name rather than by an instance, as shown below.

package com.devkuma.basic.statickeyword.ex1;

public class StudentSample {

    public static void main(String[] args) {
        Student devkuma = new Student("devkuma");
        Student araikuma = new Student("araikuma");
        Student kimkc = new Student("kimkc");

        System.out.println(++devkuma.number);
        System.out.println(++araikuma.number);
        System.out.println(++kimkc.number);

        System.out.println(Student.number);
    }
}

Execution result:

101
102
103
103

static Methods

Since static is declared on class elements, it can also be declared on methods.

These methods are called static methods. Static methods can use only static fields and local variables.

Student.java

package com.devkuma.basic.statickeyword.ex2;

public class Student {
    static int number = 100;
    String name;

    Student(String name) {
        this.name = name;
    }

    public static void printNumber() {
        System.out.println("지금 Student의 number는" + number + "입니다.");
    }
}

StudentSample.java

package com.devkuma.basic.statickeyword.ex2;

public class StudentSample {

    public static void main(String[] args) {
        Student.printNumber();
        Student.number++;
        Student.printNumber();
    }
}

Execution result:

지금 Student의 number는 100입니다.
지금 Student의 number는 101입니다.

As in the example above, a static method can be called by referencing the class name without creating an instance.

Variables That Can Be Used in static Methods

package com.devkuma.basic.statickeyword.ex2;

public class Student {
    static int number = 100;
    String name;

    Student(String name) {
        this.name = name;
    }

    public static void printNumber() {
        int localVariable = 100; // ok
        name = "a"; // error
        System.out.println("지금 Student의 number는 " + number + "입니다.");
    }
}

In the example above, no error occurs when declaring the local variable localVariable, but an error occurs for the class member variable name.

As explained earlier, static methods can use only local variables and static fields. A static method can be used without creating an instance. Normal fields are created when an instance is created, so they cannot be used in static methods.