static keyword in java With Examples

static keyword in Java is used a lot in java programming. Java static keyword is used to create a Class level variable in java. static variables and methods are part of the class, not the instances of the class.

static keyword in java


Java static keyword can be used in five cases as shown in below image.


We will discuss four of them here, the fifth one was introduced in Java 8 and that has been discussed at Java 8 interface changes.

  1. Java static variable

    We can use static keyword with a class level variable. A static variable is a class variable and doesn’t belong to Object/instance of the class.

    Since static variables are shared across all the instances of Object, they are not thread safe.

    Usually, static variables are used with the final keyword for common resources or constants that can be used by all the objects. If the static variable is not private, we can access it with ClassName.variableName

  2. Java static method

    Same as static variable, static method belong to class and not to class instances.

    A static method can access only static variables of class and invoke only static methods of the class.

    Usually, static methods are utility methods that we want to expose to be used by other classes without the need of creating an instance. For example Collections class.

    Java Wrapper classes and utility classes contains a lot of static methods. The main() method that is the entry point of a java program itself is a static method.

    From Java 8 onwards, we can have static methods in interfaces too. For more details please read Java 8 interface changes.

  3. Java static block

    Java static block is the group of statements that gets executed when the class is loaded into memory by Java ClassLoader.

    Static block is used to initialize the static variables of the class. Mostly it’s used to create static resources when the class is loaded.

    We can’t access non-static variables in the static block. We can have multiple static blocks in a class, although it doesn’t make much sense. Static block code is executed only once when the class is loaded into memory.

  4. Java Static Class

    We can use static keyword with nested classes. static keyword can’t be used with top-level classes.

    A static nested class is same as any other top-level class and is nested for only packaging convenience.

    Read: Java Nested Classes

Let’s see all the static keyword in java usage in a sample program.

Let’s see how to use static variable, method and static class in a test program.

The output of the above static keyword in java example program is:

Notice that static block code is executed first and only once as soon as class is loaded into memory. Other outputs are self-explanatory.

Java static import

Normally we access static members using Class reference, from Java 1.5 we can use java static import to avoid class reference. Below is a simple example of Java static import.

Notice the import statements, for static import we have to use import static followed by the fully classified static member of a class. For importing all the static members of a class, we can use * as in import static com.journaldev.test.A.*;. We should use it only when we are using the static variable of a class multiple times, it’s not good for readability.

Update: I have recently created a video to explain static keyword in java, you should watch it below.

By admin

Leave a Reply

%d bloggers like this: