News & UpdatesProgrammingWeb programming StoreMy Projects

Java Tutorial – 12 – Static

The static keyword is used to create fields and methods that can be accessed without having to make an instance of the class. Static (class) members only exist in one copy, which belongs to the class itself, whereas instance (non-static) members are created as new copies for each new object. This means that static methods cannot use instance members since these methods are not part of an instance. On the other hand, instance methods can use both static and instance members.

class MyCircle
  float r = 10;            // instance field
  static float pi = 3.14F; // static/class field
  // Instance method
  float getArea() { return newArea(r); } 
  // Static/class method
  static float newArea(float a) { return pi * a * a; } 

Accessing static members

To access a static member from outside the class, the class name is used followed by the dot operator. This operator is the same as the one used to access instance members, but to reach them an object reference is required. Trying to access a static member by using an object reference (instead of the class name) will give a warning since this makes it more difficult to see that a static member is being used.

public static void main(String[] args)
  float f = MyCircle.pi;
  MyCircle c = new MyCircle();
  float g = c.r;

Static methods

The advantage of static members is that they can be used by other classes without having to create an instance of the class. Fields should therefore be declared static when only a single instance of the variable is needed. Methods should be declared static if they perform a generic function that is independent of any instance variables. A good example of this is the Math class which only contains static methods and fields.

double pi = Math.PI;

Math is one of the classes that are included by default in every Java application. The reason for this is because it belongs to the java.lang package, which is always imported. This package contains classes fundamental to the Java language, such as: String, Object and System.

Static fields

Static fields have the advantage that they persist throughout the life of the application. They can therefore, for example, be used to record the number of times that a method has been called across all instances of the class. The initial value for a static field will only be set once, sometime before the class or field is ever used.

class MyCircle
  static void dummy() { count++; }    
  static int count = 0;

Static initialization blocks

A static initialization block can be used if the initialization of static fields requires more than one line, or some other logic. This block, in contrast to the constructor, will only be run once, at the same time as the static fields are initialized.

static int[] array = new int[5];
  int i = 0;
  for(int element : array) 
    element = i++;

Initialization blocks

An initialization block provides an alternative method for assigning instance fields. This block is placed on the class level, just like the static initialization block, but without the use of the static keyword. Any code placed between the brackets will be copied to the start of every constructor by the compiler.

int[] array = new int[5];
  int i = 0;
  for(int element : array) element = i++;

A class can have multiple initialization and static initialization blocks.

Recommended additional reading:
Sams - Teach Yourself Java in 24 Hours