Explain Codes LogoExplain Codes Logo

What is the use of a private static variable in Java?

java
singleton
constants
immutability
Anton ShumikhinbyAnton Shumikhin·Mar 12, 2025
TLDR

A private static variable in Java is a class-level attribute, unseen by other classes. It's perfect for internal counters or constants. These variables, whilst shared among all instances of a class, are shielded from external access, thereby ensuring encapsulation and class-specific utility.

Here's an example to track object creation:

public class ObjectCounter { // This variable is like a bouncer at a club, keeping tabs on everyone that comes in. private static int count = 0; public ObjectCounter() { count++; // incremented each time an object of this class is created. } // Who are you gonna call for counting created objects? "countBuster!" public static int getCount() { return count; } } // Usage: // Let's create objects, and see the bouncer at work. ObjectCounter obj1 = new ObjectCounter(); ObjectCounter obj2 = new ObjectCounter(); System.out.println(ObjectCounter.getCount()); // Outputs: 2

The variable count encapsulates both private and static properties, making it an excellent poster child for this concept.

A capsule for internal state

Private static variables are the go-to for maintaining internal state in a well-encapsulated manner. By restriction, only designated methods (getters/setters) within the class can interact with these variables.

The final modifier plays the role of a trusty guardian, watching over constants. Once declared final, their state is bound by the laws of immutability. For example:

// Our beloved math constant getting the "private static" treatment. public class MathConstants { private static final double PI = 3.14159; }

For static methods, private static variables are like their trusted sidekicks, available for every daring mission.

Singletons love private static variables

The Singleton pattern and private static variables share a bond that rivals peanut butter and jelly. The singleton, needing to stay unique and globally accessible, finds an ally in the private static variable:

// Universe: A singleton, because let's face it, one is enough. public class Universe { // Resident in the class' memory and hidden from prying eyes out there (private). private static final Universe instance = new Universe(); private Universe() {} // This method is like guardian of Universe. Pretty cool job huh? public static Universe getInstance() { return instance; } }

Saving precious memory

Private static variables are thrifty. They use memory efficiently as they are created only once in the JVM. Like goldfish in a fish tank, they inhabit the method area, as opposed to the commodious ocean - the heap - where instance variables dwell.

Designing APIs: a balance act

API designers need to juggle. On one hand, they want to expose useful information, on the other, they must protect sensitive data. Private static variables provide that balance. For public visibility, they don the costume of static final along with immutability, marching out with valiant clarity.

Type-level over instance-level

Static variables speak for the class, not individual objects. An Object factory class might employ a private static counter to tally the produced instances - this would be type-level information.

Practical use-cases you'll love

Here are a handful of real-life scenarios for private static variables:

  • Configuration Settings: Holding application-wide configuration settings that are read once and used everywhere.

  • Utility class counters: Counting operations or resource utilizations within utility classes for monitoring.

  • Singleton State: Storing shared state in classes following the Singleton Pattern.

  • Constants: Reducing magic numbers or literals by defining them as static finals.