Static keyword in Java

It is one of the most widely used keywors of the Java programming language. It can be used to create:

  1. Static Variables (also called Class Variables)
  2. Static Methods (also called Class Methods)
  3. Static Blocks
  4. Static Nested Classes

It has some other uses as well, which you will see towards the end.

A class member that has been declared static, can be used independently of the objects of that class. When the objects of a class are created, no copies of the static members are made, instead all objects share the same copy of the static members.


1. Static Variables or Class Variables

An instance variable when qualified with the static keyword, is called static variable. Syntax to declare a static variable is:

Note: You can drop the access modifier, in that case the class variable will have default access level i.e. acessible within the entire package.

Some quick facts about static variables are:

  • Java maintians only one copy of a static variable. All the objects of a class share the same copy of that static variable. So they are also called class variable.
  • Memory allocation for them happens only once, when the class is loaded in the memory.
  • They can be accessed directly from static and non-static methods.
  • There is no need to instiantiate an object to access them. You can acces them using their class name. However, You can access a static variable using objects as well but you don’t have to.

Example 1: Java maintains only one copy of a static variable for all objects.

The output is :

All these three individuals were supposed to be from three different countries. But we used static variable for the country. So all three objects of the Citizen class shared the same copy of country field which was over-written again and again. On the contary each object had its own name field.


Example 2: Static variables can be accessed using class name

To assign a value to the static variables, you don’t need to instantiate an object. They belong to the class, and can be assigned a  value using the class name. Similarly their value can be accessed using the class name only.

The output is:

 


2. Static Methods or Class Methods

When a method is qualified with the static keyword, it is called a static method or class method. Syntax to declare a static method is:

Methods declared as static have several restrictions:

  • They can directly access only static data.
  • They can directly call only static methods.
  • They cannot use this or super keyword inside their bodies.

However, they can access non-static data and methods using object instantiation.


Example 1: A static method can access static variables and methods directly

A static method can access static variables and static methods directly but it cannot access non-static variables and methods directly. You can access them indirectly by creating objects first and then using the dot operator.

The output is:

 


Example 2: Inside the class, Static methods can be accessed directly with name

From inside the class, a static method can be called even without the class name. Like:

The output is :

 


Example 3: Outside the class, Static methods can be accessed by class name

From outside the class, a static method can be called using the class name. No object instance has to be created.

The output is :

 


3. Static Blocks

You can make any block of code static by prefixing it with the static keyword like:


Example 1: Initialization of static variables inside static block

A static block of code is used to initialize the static variables. It is executed only once when the class is loaded. If needed, you can do some computation inside this block before initializing the static variables.

The output is:

Note that only static members of a class are accessible from a static block. If you try accessing a non-static member of a class, You will get compile time error.


Example 2: Multiple Static Blocks

If your code has more than one static block then they are executed in the order they are defined(one after the other).

The output is:

Note that you can use static blocks to initialize the non-static data as well. The age field was first initialized to 16 and then to 20.


4. Static Nested Classes

A normal class cannot be declared static. However, a nested class can be declared static, like:

Based on this, there are two types of nested classes:

  1. Static nested class : A nested class qualified with the static keyword.
  2. Non-static nested class (also called Inner class) : A nested class which has not been qulaified with the static keyword.
  • A static nested class cannot access non-static member of its outer class directly.
  • From the outer scope, a static class can be accessed using the class name itself. There is no need for an object reference of the outer class.

The output is:

Notice the way static nested class has been used in the example above.

  • To make an object of the MyClass1 from inside the Outer class, You can also use:

  • But to make an object of the MyClass1 from outside the Outer class, you must use:

 


Generics and static keyword

Static methods can be made generic. They can declare their own type parameter. But they cannot use the type parameter of the enclosing class.

In fact no static member of a class can use the type parameter of the enclosing class. For example, both the variable declaration and the method declaration is wrong in the example below.

But the example below is ok.


Example: static methods can have their own type parameters:

The output is:

 


Miscellaneous uses of Static Keyword

1. Static Import

To import the static members of a class or an interface, you must follow the import keyword in an import statement by static. Like:

It imports the staitc method sqrt defined inside Math class in java.lang package. To import all the static members defined in the Math class of the java.lang package, use:


During static import, you can use the static members directly by their names, without qualifying them with their class name. Overall, it simplifies and shortens the syntax for use of static members:

The output is:

 


1. To create library

java.lang.Math has a number of static methods and constants defined for mathematical calculation. You can also create your own library. For example, the code below creates a library of common use mathematical formulae.

It is built on top of the methods and constants defined inside java.lang.Math class. It creates a class named Formulae inside basics backage.

To use the formulae defined inside the basics.Formulae class, you first have to import the static members defined there.

The output is :

 


Summary

  • Static Variables :
    • Java maintains only one copy of the static variables.
  • Static Methods:
    • A static method can access static methods and data directly.
    • To access non-staitc data they need to create objects first.
    • Note: A non-static method can access both static and non-static members directly.
  • Static Blocks :
    • Static blocks are run only once when the class is loaded.
    • It can initialize both static and non-static fields.
  • Static Class :
    • The outer-most class cannot be made static but a nested class can be declared static.
    • A static nested class can access only static members of the outer class.
  • Generics :
    • A static member cannot use the type parameter of the outer class
    • but it can define its own type parameter.
  • Micellaneous :
    • You have to use static keyword to import the static members of a class.
    • You can also use static to make your own library.