Java Static variable (AKA class fields) Tutorial

In this section, we will learn what the static variable is and how it works in Java.

What is Static Variable in Java?

Every time we create an object from a class, the member of that class will be copied into a new location in the memory and so each object of that class has its own memory space and we can say that objects are independent from each other.

This means if we change the value of any attribute in an object, it won’t change the same attribute in another object!

In short, objects of one class have their own copy of the members and so they are independent.

But sometimes we want to have a variable or multiple variables that should be shared between all the objects of the same class.

For example, if the object A changed the value of such a variable, the object B that is created from that same class, should also see this change as well and be able to change the value if needed.

This is where `static variable` comes in.

Basically, we can turn a member variable (AKA attribute or field) of a class into static and in that case, no-matter how many objects we create from that class, only one copy of such variable will be created and is shared between all the objects.

Note: the memory space of objects is in a place called Heap, but the memory space of static variables is in a location called METASPACE.

How to declare a static variable in Java?

Let’s see how we can create a static variable:

Access-specifier static data-type variable-name;

As you can see, the `static` keyword comes between the `access-specifier` and `data-type`.

Notes to consider when creating static variables:

  • Only attributes of a class can be declared as `static`. This means we can’t turn a local variable inside a method into `static`.
  • A static variable can only be put inside a static method. If we use such variable inside a non-static method, we will get compile time error.

Note: if we use the name of the class before the static-variable, in that case a static variable can be used inside a non-static method as well.

How to call a static variable in java?

A static variable can be accessed directly via the class that is being declared in. For example, if the name of the class is `A` and the name of the variable is `sV` then this is how we can access the variable:

A.sV;

Note: the access specifier of the static variable should be `public` so that it is accessible via the name of the class.

Objects that are created from a class that has static variable can access such a variable (of course the access specifier of the variable should be public as well). But usually it’s a good practice to only access such variable via the name of the class only.

Example: static variables in static method

public class Simple {
    public static int sCount = 0;
    public int count = 0;
    public static void main(String[] args) {
        Simple smp1 = new Simple();
        Simple smp2 = new Simple();
        Simple smp3 = new Simple();
        smp1.sCount = 10;
        System.out.println(smp3.sCount);
        Simple.sCount = 100;
        System.out.println(smp2.sCount);

        smp1.count = 1000;
        System.out.println(smp2.count);
    }
}

Output:

10

100

0

In this example, we have one static variable named `sCount` and that is declared inside the `Simple` class. Also, a non-static variable named `count`.

Now within the body of the `main` method, we’ve created 3 objects of type `Simple` class. Here, the `count` variable is copied for each of these objects, but the `sCount` is shared between all three objects.

This means when we changed the value of the `sCount` variable in the program, all objects saw this change as well.

But when we change the value of the `count` variable in one object, it is only applying to that object alone and not the others because they have their own copy of this variable.

Note: the use of `static` keyword is also in other places like with `class`, `methods` and `block` as well. So to learn more about the static keyword, make sure you check the static class, static method and static block too.

Example: accessing static variables via instance of class

class Main{

    public static int num = 0; 

    public static void main(String[] args) {
        Main main = new Main();
        main.num++;
        Main.num++; 
        System.out.println(num);

    }   
}

Output:

2

Note that using instances of a class, we can access the static members as well.

Static final variables

As static variable can also become final as well. That means after the variable is declared and initialized, its value can’t change anymore!

Example: static and final variables

class Main{

    public static final int num = 1; 

    public static void main(String[] args) {
        Main main = new Main();
        Main.num++; 
        System.out.println(num);

    }   
}

Output:

Main.java:7: error: cannot assign a value to final variable num

As you can see, because we’ve tried to change the value of a final variable, we got error.

Instance variable Vs Static variable

In short, instance variables are copied for each object that is created from the target class and that means each object will have its own independent variable.

On the other hand, when creating a static variable in a class, there will be only one copy of such variable! So it means the variable is shared between the rest of instances.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies