Can you provide an example of a static variable and explain its usage?

Let's consider a scenario where we are building a class Car. Every car object created will have a specific model and year, but all cars have the same number of wheels. In this case, we can use a static variable to store the number of wheels, as this value is the same for all Car objects.

Code Example:

public class Car {
    // Static variable - shared by all instances
    static int numberOfWheels = 4;  // All cars have 4 wheels

// Instance variables - unique to each instance
String model;
int year;

// Constructor to initialize instance variables
public Car(String model, int year) {
this.model = model;
this.year = year;
}

// Method to display car information
public void displayInfo() {
System.out.println("Car Model: " + model);
System.out.println("Car Year: " + year);
System.out.println("Number of Wheels: " + numberOfWheels); // Static variable
}

public static void main(String[] args) {
// Create two Car objects
Car car1 = new Car("Toyota", 2020);
Car car2 = new Car("Honda", 2018);

// Display information for both cars
car1.displayInfo();
System.out.println();  // Print an empty line
car2.displayInfo();

}


}  

Explanation:

  1. Static Variable:
  2. static int numberOfWheels = 4;: This is a static variable that is shared across all instances of the Car class. Since all cars have 4 wheels, we don't need to duplicate this information for every car object. The static variable ensures that only one copy of this variable exists in memory, regardless of how many Car objects are created.
  3. Since it is static, the numberOfWheels variable is initialized when the class is loaded into memory, not when individual objects are created.

  4. Instance Variables:

  5. String model and int year are instance variables that are unique to each Car object. These variables hold the specific model and year of each car and are initialized through the constructor when new objects are created.

  6. Accessing Static Variables:

  7. Inside the displayInfo() method, the static variable numberOfWheels is accessed directly because it's available at the class level. It is shared by both car1 and car2.
  8. You can also access numberOfWheels using the class name, like this: Car.numberOfWheels.

  9. Creating and Using Objects:

  10. In the main() method, two Car objects (car1 and car2) are created with different models and years.
  11. When the displayInfo() method is called on both objects, each object displays its unique model and year, but both share the same value for the number of wheels, thanks to the static variable.

Output:

Car Model: Toyota
Car Year: 2020
Number of Wheels: 4
Car Model: Honda  

Car Year: 2018

Number of Wheels: 4

Usage of Static Variables:

  1. Shared Data Across Instances:

Static variables are useful when you want to share data across all instances of a class. In this case, numberOfWheels is shared by all Car objects, making it more efficient to store common data at the class level.

  1. Memory Efficiency:

Since static variables are stored only once in memory (at the class level), they help reduce memory consumption when the same data applies to all instances of the class.

  1. Global Access via Class Name:

Static variables can be accessed without creating an instance of the class, which makes them convenient for storing global or constant values. For example, you could access numberOfWheels using Car.numberOfWheels anywhere in your program.

Summary:

  • A static variable, like numberOfWheels in the example, is shared across all objects of the Car class.
  • Static variables are ideal when you want to store class-level information that is common for all instances.
  • They can be accessed directly or via the class name and help reduce memory usage by avoiding redundant copies for each instance.

In conclusion, static variables are useful for holding values that are common across all instances of a class, such as constants or shared data, and can be accessed without the need to create an object.