Java’s static variables are variables or fields that are common to all object of a class. Unlike instance variables, objects of the same class have their own distinct copies of the variables and values. In other words, a static variable is shared among objects of the same class. You can declare static variables (or static fields or class variables) by adding static
modifier to the variables declaration.
For example:
public class Account { // Account balance private int balance; // Number of account in the system private static int numberOfAccounts = 0; ...... }
Instances (objects) of class Account
will have their own copy of balance but all account objects share the same numberOfAccount
.
Java’s static methods or class methods are methods that can be called without instantiate an object. Static methods also have static
modifier to the methods declaration. Although you can call static methods with an object reference but it is not recommended as it does not make it clear that they are class methods. Static methods cannot access to instance variables and methods. A common use of static methods is to access to the static variables of it’s class.
For example:
public class Account { // Account balance private int balance; // Number of account in the system private static int numberOfAccounts = 0; // Constructor public Account(int balance) { this.balance = balance; // Calling static method incrementNumberOfAccounts(); } // Close account public void close() { decrementNumberOfAccounts(); }
// Get number of accounts in the system public static int getNumberOfAccounts() { return numberOfAccounts; } // Incement number of account private static void incrementNumberOfAccounts() numberOfAccounts ++; } // Decrement number of account private static void decrementNumberOfAccounts() numberOfAccounts ++; } ...... }
From the above example you can know the number of account objects have been created by calling Account.getNumberOfAccounts()
.
Java’s static constructor or static initialization block is a normal block of code enclosed in braces, { }
, and preceded by the static
keyword. The block of code can be appeared in any place of the class body. Like a normal constructor, it is used to initialize static variables. Although you can initialize a static variable inline, but sometime you may need more codes for more complicated situation to initialize the static variables.
For example:
public class Jackpot { // Jackpot value private int value; // Extra jackpot value, with // 70% of chances to have zero values and // 10% of chances to have 100 and // 10% of chances to have 1000 and // 10% of chances to have 10000 and private static int extra; // Static constructor static { Random random; random = new Random(); switch(random.nextInt(10)) { case 7: extra = 100; break; case 8: extra = 1000; break; case 9: extra = 10000; break; default: extra = 0; } } ...... }
Do remember that, Java’s static initialization blocks are called in the order that they appear in the source code.
[…] Java programmers, including my self, might have mistaken that the portion of code that initializes the static variables of a class […]