What are Static variables?
Static variables or class variables are shared among all instances of a class and can be accessed and modified without creating an instance of the class. Let’s take a deep dive to understand the uses and implementation of static variables in Java.Static variable example in Java
One of the most common uses for static variables is to share among all instances of a class. For example, a counter variable that keeps track of the number of instances created, or a CONSTANT variable that holds a value that never changes. Consider a class that represents a bank account. The following code snippet shows how a static variable nextAccountNumber can be used to assign unique account numbers to each new account:Example
// BankAccount class definition
class BankAccount {
// Declare static variable nextAccountNumber
static int nextAccountNumber = 1001;
// Declare instance variables for accountNumber and balance.
int accountNumber;
double balance;
// BankAccount constructor that assigns unique account number to each instance
BankAccount() {
// Assign the current value of nextAccountNumber to accountNumber.
accountNumber = nextAccountNumber;
// Increment nextAccountNumber for the next BankAccount object.
nextAccountNumber++;
}
// Method to get the account number of this BankAccount object.
int getAccountNumber() {
return accountNumber;
}
// Method to get the balance of this BankAccount object.
double getBalance() {
return balance;
}
// Method to deposit an amount into this BankAccount object.
void deposit(double amount) {
// Increase balance by the amount deposited.
balance += amount;
}
// Method to withdraw an amount from this BankAccount object.
void withdraw(double amount) {
// Decrease balance by the amount withdrawn.
balance -= amount;
}
}
// Main method definition
class Main {
static void main(String[] args) {
// Create three BankAccount objects: account1, account2, and account3.
BankAccount account1 = new BankAccount();
BankAccount account2 = new BankAccount();
BankAccount account3 = new BankAccount();
// Display the value of the static variable nextAccountNumber after creating each BankAccount object.
System.out.println("Value of nextAccountNumber after creating account1: " + account1.getAccountNumber());
System.out.println("Value of nextAccountNumber after creating account2: " + account2.getAccountNumber());
System.out.println("Value of nextAccountNumber after creating account3: " + account3.getAccountNumber());
}
}
Output
Value of nextAccountNumber after creating account1: 1001
Value of nextAccountNumber after creating account2: 1002
Value of nextAccountNumber after creating account3: 1003
Example
public class Singleton {
// using static variable in the singleton class
private static Singleton instance;
private Singleton() { }
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
public void displayMessage() {
System.out.println("This is a singleton class");
}
public static void main(String[] args) {
Singleton singleton1 = Singleton.getInstance();
singleton1.displayMessage();
Singleton singleton2 = Singleton.getInstance();
singleton2.displayMessage();
Singleton singleton3 = Singleton.getInstance();
singleton3.displayMessage();
}
}
Output
This is a singleton class
This is a singleton class
This is a singleton class
In this example, the main method creates three instances of the Singleton class by calling the getInstance() method. The getInstance() method returns the instance of the class and creates a new instance if one does not exist. The displayMessage method just prints a message. Then the main method calls the displayMessage method for each instance.
As you can see, even though we are creating three instances, only one object is created and shared among all the instances. It proves that only one instance of the class is created and shared among all the instances and the static variable 'instance' is holding.
It's important to remember that the value of a static variable is shared among all instances of a class, so changes to the variable made by one instance will be visible to all other instances. Careful consideration should be given to static variables to avoid potential conflicts and bugs in your code. For example, using a static variable to hold a value that is specific to an instance of a class, such as an account balance, is not recommended.
To reinforce what you learned, we suggest you watch a video lesson from our Java Course