Java Static Keyword Example

16 Flares Twitter 0 Facebook 1 Google+ 15 Pin It Share 0 LinkedIn 0 Filament.io 16 Flares ×

In Java, the static keyword can be applied to four programming artifacts They are

  1. static variable (a.k.a : class variable)
  2. static method (a.k.a: class method)
  3. static block ( useful for initialization during class loading)
  4. static nested class

Static Variable

When many instances of a class are created, each instance will have its own copies of instance variables. That means number of objects created from the same blue print ( class ) has its own distinct copies of instance variables. Some times we may need few variables to be common to all instances.  This is realized using the static keyword. Let us take an example

In the above class, the pixelDensity, size and serialNum can be different for different instances of the SonyTelevision. But the MANUFACTURER is common and we don’t need every instance to have its own distinct copies of the MANUFACTURER variable. We want all instances to share the same object. In such situations static keyword can be used. By declaring that the MANUFACTURER, variable is static, it is shared across all instances of the class. As the field is static, it can be referred without the need for any instance of the class.

Static fields can be referred using just the class name

ClassName.fieldName

SonyTelevision.MANUFACTURER;

As the static variable is shared across all instances of the class, it makes provides efficient memory usage. Hence reducing the memory usage is an added benefit.

Learn by Example: Static Variable

The PageCounter class is used to tract the no of times a page is visited. Here we have a count variable and is NOT defined as static.  Thus the above program is not correct and for each instance of the PageCounter class, count is always 1. The output of the above program will be


1
1
1

What we really want it the counter to get incremented and all instances to get the incremented count. Hence we make the count variable static.

Output of the modified program will be

3

This is the behavior we need !. The static variable counter, got shared between all instances and only a single copy was kept in memory.

static variable memory structure

 Static Method

  1. A static method belongs to the class and not to the instances of the class.
  2. A static method can be invoked by specifying the classname.method name syntax. No need to create instances of the class
  3. static variables can be accessed and modified by static methods

Accessing / Invoking static methods

ClassName.methodName(args)
There are few  restrictions for the static method. They are:

  • static method can not invoke non-static methods directly. 
  • this and super cannot be used in static context.

This makes sense as this keyword refers to a particular instance and with static is not bound to any instance.

This program will throw a compiler error as the main method ( static method ) is accessing a non static variable count. This is not allowed in Java. Let us take one more example to understand the full usage and restrictions of static methods and variables.

Few important points

  • static methods can be invoked from non static methods
  • static methods cannot invoke non static methods
  • static variables can be invoked from non static methods
  • non static variables cannot be accessed from static methods directly

Static Block

A static initialization block is a normal block of code enclosed in braces, { }, and preceded by the static keyword.
Usage
static {
// code
}
A class can have any number of static initialization blocks. The JVM guarantees that static initialization blocks are called in the order that they appear in the source code. 

Instead of static blocks, it is recommended to use private static methods for initialization. The added advantage is these can be called specifically if the variable needs to be re-initialized again.

Static Inner Classes

Like all inner classes, a static nested class is associated with its outer class.  The main difference is a static nested class cannot access the instance variables or methods of the enclosing class directly. Refer this stackoverflow post to know more.

References

JVM Internals

Stackoverflow

Oracle Java Tutorial

  • Dieter

    You should also add the Stativ Import.

    • Dieter

      static import, ofc