Skip to content
On this page

Static fields and static methods

Fields defined in a class are called instance fields. The characteristic of instance fields is that each instance has independent fields, and fields with the same name in each instance do not affect each other.

There is also a kind of field, which is a field modified with static , called a static field: static field .

Instance fields have their own independent "space" in each instance, but static fields have only one shared "space", which is shared by all instances. For example:

java
class Person {
  public String name;
  public int age;
  // Define static field number:
  public static int number;
}

Let's take a look at the following code:

java
public class Main {
    public static void main(String[] args) {
        Person bob = new Person("bob", 12);
        Person jack = new Person("jack", 15);
        bob.number = 88;
        System.out.println(jack.number);
        jack.number = 99;
        System.out.println(bob.number);
    }
}

class Person {
    public String name;
    public int age;

    public static int number;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

For static fields, no matter which instance's static fields are modified, the effect is the same: the static fields of all instances are modified, because the static fields do not belong to the instance:

        ┌──────────────────┐
bob  ──▶│Person instance   │
        ├──────────────────┤
        │name = "bob"      │
        │age = 12          │
        │number ───────────┼──┐    ┌─────────────┐
        └──────────────────┘  │    │Person class │
                              │    ├─────────────┤
                              ├───▶│number = 99  │
        ┌──────────────────┐  │    └─────────────┘
jack ──▶│Person instance   │  │
        ├──────────────────┤  │
        │name = "jack"     │  │
        │age = 15          │  │
        │number ───────────┼──┘
        └──────────────────┘

Although instances can access static fields, they actually point to static fields of Person class . So, all instances share a static field.

Therefore, it is not recommended to use intance.static field, because in Java programs, instance objects do not have static fields. In the code, the instance object can access the static field only because the compiler can automatically convert it to [class name].[static field] according to the instance type to access the static object.

It is recommended to use class names to access static fields. Static fields can be understood as fields that describe class itself. For the above code, a better way to write it is:

java
Person.number = 99;
System.out.println(Person.number);

Static method

If there are static fields, there are static methods. Methods modified with static are called static methods.

Calling instance methods must pass an instance variable, while calling static methods does not require an instance variable and can be called through the class name. Static methods are similar to functions in other programming languages. For example:

java
public class Main {
    public static void main(String[] args) {
        Person.setNumber(99);
        System.out.println(Person.number);
    }
}

class Person {
    public static int number;

    public static void setNumber(int value) {
        number = value;
    }
}

Because the static method belongs to class and not to the instance, within the static method, you cannot access this variable or instance fields. It can only access static fields.

Static methods can also be called through instance variables, but this is just the compiler automatically rewriting the instance into the class name for us.

Normally, you will get a compilation warning when accessing static fields and static methods through instance variables.

Static methods are often used in utility classes. For example:

  • Arrays.sort()
  • Math.random()

Static methods are also often used in helper methods. Notice that the entry point of the Java program main() is also a static method.

Static fields of interface

Because interface is a purely abstract class, it cannot define instance fields. However, interface can have static fields, and static fields must be of final type:

java
public interface Person {
    public static final int MALE = 1;
    public static final int FEMALE = 2;
}

In fact, because the fields of interface can only be of public static final type, we can remove all these modifiers. The above code can be abbreviated as:

java
public interface Person {
    // The compiler will automatically add public static final:
    int MALE = 1;
    int FEMALE = 2;
}

Practise

Add a static field count and static method getCount() to the Person class to count the number of instances created.

Summary

Static fields are fields "shared" by all instances, and are actually fields belonging to class ;

Calling static methods does not require an instance and cannot access this , but static fields and other static methods can be accessed;

Static methods are often used in utility classes and helper methods.

Static fields and static methods has loaded