Static is a keyword in Java that can be applied to variables, methods, classes, and blocks. This post provides an overview of the behavior of variables, methods, classes, and blocks of code in Java when the static keyword is applied to them.

1. Static variables

The static variables are also known as class variables. When static keyword is applied to a variable, it means that the variable belongs to the class and not an instance of the class. The static variables has the following characteristics:

  1. There is only one copy of the static variable in memory, which is shared by all instances of the class.
  2. Static variables are initialized before any of the instance variables.
  3. Static variables can be used when some value needs to be shared across all class objects. To ensure the value never gets changed after its initialization, declare the static variable as final.
  4. A static variable can be accessed directly using the class name without instantiating the class in which they are defined. Use the notation ClassName.staticVariableName to access the static variable. Within the same class, they can be accessed directly.
  5. The standard output stream System.out is an excellent example of static variable in Java. Here out is declared static in the System class.

 
Consider the following code which has a static variable i defined in class A. It can be directly accessed by the name i inside the class A and with A.i outside the class A.

Download  Run Code

2. Static methods

When a static keyword is applied to a method, the method can be accessed without creating an instance of the class. In other words, only a single copy of the static method is shared between all instances of a class. The static methods has the following characteristics:

  1. Static methods cannot access non-static variables or methods without having an instance of its class.
  2. A static method cannot be overridden in Java since overriding require late binding and static methods are bonded by compiler (early binding).
  3. A static method cannot refer to this and super keywords since they are associated with a particular instance.
  4. A static method can be called directly inside the class they are declared in and outside the class using the notation ClassName.staticMethodName().
  5. The main method is an excellent example of static method in Java. The main() method is declared static as the JVM (Java Virtual Machine) calls it before any objects are created. Since it is static, it can be directly invoked by the application using the class name.
  6. A static method should be used when the method doesn’t depend on the individual characteristics of the class. Most of the utility and factory methods are usually static in Java.

 
Consider the following code having a static method f() defined in class A. It can be directly accessed by name f() inside the class A and with A.f() outside the class A without need for any instance.

Download  Run Code

3. Static class

Java provides support for the static class. But the outermost class cannot be static. A class can be static only if it is a nested class. The static class has the following characteristics:

  1. A static nested class cannot access the non-static variables and methods of the outer class. It can only access the static members of the outer class.
  2. A static nested class can have methods and variables of its own. They can even share the same name as members of the outer class.
  3. A static nested class doesn’t need reference of the outer class.

 
Consider the following code, which demonstrates the working of nested static class in Java:

Download  Run Code

Output:

Inside non-static method of the inner class
Inside static method of the outer class
A static field of the outer class

4. Static blocks

A static block is a block of code enclosed in curly braces, preceded by the static keyword, and can appear anywhere within the class body. They are also known as Static initialization blocks. The static blocks has the following characteristics:

  1. Static blocks are used to initialize static variables.
  2. Static blocks are executed when the class is loaded in the memory by the JVM.
  3. The code inside a static block is executed only once before execution of the main() method.
  4. A class can have multiple static blocks, and they will execute in the same sequence as they appear in the class source code.
  5. JVM usually combines all static blocks into single static block and then executes them.

 
Consider the following Java program, which has multiple static blocks. They will be executed in the same order from top to bottom in which they are defined before the main() method.

Download  Run Code

Output:

Inside first static block
Inside static method
This value never changes
Inside second static block
main() method is executed last

That’s all about static variables, static methods, static classes, and static blocks in Java.